In this conclusion to a three-part series on nesting JavaScript functions, we see how nested functions can be used as object types. We shall make it simple. We shall consider only the case where there is only one level of nested functions.
Nested JavaScript Functions as Objects - My method of coding JavaScript Constructor Function (Page 5 of 5 )
From my experience, documentation for JavaScript objects is scarce. You have many documents about JavaScript coding, but many of these documents do not give you an in-depth analysis of the JavaScript Object. I have written an article about adding and deleting properties and methods for JavaScript objects. I have also written this one. After reading the basics of the JavaScript object from somewhere, you can read these articles I have mentioned for an in-depth analysis.
JavaScript is like a complementary language; it is used to make a web page active instead of static. The desire to make the web page more active is increasing. More and more people prefer to work with objects than to work with functions and variables.
Now that we have discovered that nested functions in a constructor function can be assigned as methods, I will give you my way of coding the constructor function in JavaScript. This is the proposal:
function ObjectType(parameters)
{
//property declared (and assigned)
//property declared (and assigned)
//property declared (and assigned)
…
//Method declared and assigned
//Method declared and assigned
//Method declared and assigned
…
//Corresponding method defined
//Corresponding method defined
//Corresponding method defined
}
I have not considered methods that are assigned to external functions. You start by declaring the properties. You can assign values to the properties if you want to. Next you declare the methods and assign them. The functions for the methods are inside the constructor. So when you declare the methods, you have to assign the functions (names) to the method names. The definitions of the functions for the methods follow the method declarations inside the constructor function.
The methods you define like this are methods that you know will always be object type (class) methods (objects can still change them, though). The function definitions have been given last. This makes your work neat. Defining your object type the way I have proposed keeps your object intact. The properties and methods are in one block. This is in contrast to the case where the functions for the methods are outside the constructor function.
In this part of the series we have seen the role nested functions can play in constructor functions (object types).
In the first two parts of the series we had just one chain of nesting functions; that is, a function inside a function inside a function (and so on). In this part of the series we have considered the situation where a function can nest other functions, but these nested functions are of the first level of the outermost functions. We have seen that these nested functions can be used as methods in object types.
We have come to the end of the series. We have not considered the tree-like nature of nested functions. The tree structure would occur when you have one or more nested functions at the same level immediately below the outermost functions; each of these functions may have one or more of its own immediate same level functions, and so on.
I hope you appreciated the series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.