Badblog

welcome to our blog

We are Learncodz.


Posts

Comments

The Team

Blog Codz Author

Connect With Us

Join To Connect With Us

Portfolio

    Posted by: Anonymous Posted date: 10:05 / comment : 0



    When any object is instantiated, the constructor property is created behind the scenes as a property of that object or instance. This property points to the constructor function that created the object. In the next code sample, we create an Object()object, stored in the foo variable, and then verify that the constructor property is available for the object we created.
    Sample: sample23.html
    This can be useful: If Im working with some instance and I cant see who or what created it (especially if it is someone elses code), I can determine if its an array, an object, or whatever.
    In the following sample, you can see that I have instantiated most of the pre-configured objects that come included with the JavaScript language. Note that using literal or primitive values does not mean that the constructor pointer is not resolved when the primitive literal value is treated as an object.
    Sample: sample24.html
    The constructor property also works on user-defined constructor functions. In the following sample, we define a CustomConstructor() constructor function, then using the keyword new, we invoke the function to produce an object. Once we have our object, we can then leverage the constructor property.
    Sample: sample25.html
    You might be confused as to why primitive values have constructor properties that point to constructor functions when objects are not returned. By using a primitive value, the constructor is still called, so there is still a relationship with primitive values and constructor functions. However, the end result is a primitive value.
    If you would like the constructor property to log the actual name of the constructor for user-defined constructor functions, you have to give the constructor function an actual name (e.g., var Person = function Person(){};).
    By using the instanceof operator, we can determine (true or false) if an object is an instance of a particular constructor function.
    In the next sample, we are verifying if the object InstanceOfCustomObject is an instance of the CustomConstructor constructor function. This works with user-defined objects as well as native objects created with the new operator.
    Sample: sample26.html
    One thing to watch out for when dealing with the instanceof operator is that it will return true any time you ask if an object is an instance of Object, since all objects inherit from the Object() constructor.
    The instanceof operator will return false when dealing with primitive values that leverage object wrappers (e.g., 'foo' instanceof String // returns false). Had the string 'foo' been created with the new operator, the instanceof operator would have returned true. So, keep in mind that instanceof really only works with complex objects and instances created from constructor functions that return objects.
    In JavaScript, objects can be augmented at any time (i.e. dynamic properties). As previously mentioned, and to be exact, JavaScript has mutable objects. This means that objects created from a constructor function can be augmented with properties.
    In the following code sample, I create an instance from the Array() constructor and then augment it with its own property.
    Sample: sample27.html
    This could be done with Object()RegExp(), or any of the other non-primitive constructors—even Boolean().
    Sample: sample28.html
    Adding properties to objects created from a constructor function sometimes occurs. Remember, object instances created from constructor functions are just plain old objects.
    Keep in mind that besides their own properties, instances can have properties inherited from the prototype chain. Or, as we just saw in the previous code sample, properties added to the constructor after instantiation. This highlights the dynamic nature of objects in JavaScript.
    Do not confuse the general term "JavaScript objects," which refers to the notion of objects in JavaScript, with Object() objects. An Object() object (e.g., var myObject = new Object()) is a very specific type of value expressed in JavaScript. Just as an Array() object is a type of object called array, an Object() object is a type of object called object. The gist is that the Object() constructor function produces an empty generic object container, which is referred to as an Object() object. Similarly, the Array() constructor function produces an array object, and we refer to these objects as Array() objects.
    In this book, the term "JavaScript objects" is used to refer to all objects in JavaScript, because most of the values in JavaScript can act like objects. This is due to the fact that the majority of JavaScript values are created from a native constructor function which produces a very specific type of object.
    What you need to remember is that an Object() object is a very specific kind of value. Its a generic empty object. Do not confuse this with the term "JavaScript objects" used to refer to most of the values that can be expressed in JavaScript as an object.

    icon allbkg

    Tagged with:

    Next
    Newer Post
    Previous
    Older Post

    No comments:

Comments

The Visitors says
Download Free Software Latest Version