JavaScript function and Object detailed description

Source: Internet
Author: User
Tags anonymous constructor extend

Function and Object


var n = new Number (1)
N.prototype-> undefined
Why N.prototype for undefined? I was puzzled by the problem. Because we know that the new function (). Prototype is object. This issue raises my interest in function and object research.


The diagram above is the relationship between function and object that can be validated by code.
The Blue line is an instance of the constructor of the class
Orange Line is an instance of a class
The Green Line is an instance of the prototype of the class
Example of a prototype of a constructor with a black line as a class
Here's how to prove:
1.function instanceof Object-> True
2.object instanceof Function-> True
At this time the orange relationship was established. It can also be concluded that function and object are examples of themselves.
3.function.constructor = = function-> true
4.object.constructor = = function-> true
This blue relationship is established, and it is learned that object is constructed by function, and function is constructed by itself.
5.function.prototype instanceof Object-> True
6.object.prototype instanceof Object-> False
Green relationship is established, so that is, the function of the prototype object is an instance of object, can be based on 3, 4, 5 of the relationship between the black relationship:
7.function.constructor.prototype instanceof Object-> True
8.object.constructor.prototype instanceof Object-> True

No matter which instance we create Stringbooleanobjectnumber, when you attempt to use the new object (), you will get undefined, which means that this property is not included in this instance, but is actually implied by an internal [[ Prototype]], its value is this type of prototype object, such as: Object.prototype, but we can not use it. But it can still be obtained in another way:
var s = new string ();
S.constructor.prototype-> Object
This is the prototype object of string.
S.constructor.prototype.hello = ' Hello ';
New string (). Hello-> ' hello ';
This allows you to extend the prototype object through an instance. The effect is equal to String.prototype.hello = ' Hello '.
It is learned from the above proof that you can access the Function.prototype through any one instance.
S.constructor.constructor.prototype.newproperty = ' NewProperty ';
New function (). NewProperty-> ' NewProperty ';
All of the function instances you have created will have this newproperty. But it is never possible to get object.prototype through an instance (an instance of object class can get Object.prototype from the above method, such as: New Object (). Constructor.prototype, But instances of non-object classes cannot get Object.prototype), Because all instances do not directly have prototype values of Object.prototype properties, if you try to use constructor to get Object.prototype, Will fall into the function.constructor cycle, because the constructor of the function is himself, thus protecting Object.prototype, because all objects are based on it, and if they can be accessed from anywhere, The program could be in deep chaos.
In fact, the purpose of using prototype is to implement the property sharing and inheritance of classes (Class). We extend the property on the prototype object, and its instance and subtype all have this property. And for the unusual new function (). prototype, the prototype here is not object.prototype. There is one sentence in the specification that can be explained:
Each function (function object) automatically creates a prototype property because this function (function object) may be treated as a constructor.
That is, the function is treated as constructor when you use the New keyword.
Use the new function (). Prototype.constructor can get an anonymous function. This anonymous function is actually created by the new function (). The new function () generates a function anonymous () {}. If you use this form: The new new function (), you will get an instance of the anonymous class. Let's look at a piece of code:
function foo () {}.prototype.constructor-> function foo () {}
In other words, the prototype object of function foo () {} has a constructor property that points to itself. Further.
function foo () {};
Foo.prototype.constructor-> function foo () {}
New Foo (). constructor = = Foo.prototype.constructor-> true
New Foo () instanceof Foo.prototype.constructor-> True
Foo.prototype instanceof Foo-> false

Alert (function instanceof function);//true
Alert (function instanceof object);//true
Alert (object instanceof function);//true
function foo () {};
var foo = new Foo ();
Alert (foo instanceof foo); True
Alert (foo instanceof function); False
Alert (foo instanceof object); True
Alert (foo instanceof function); True
Alert (foo instanceof object); True

object is the top-most objects, all objects will inherit the object's prototype, but you also have to know explicitly that object is also a function object, so that object is constructed by function

function foo () {};
var foo = new Foo ();
The constructor for Foo Foo
Alert (foo instanceof foo); True
But function is not the constructor of Foo
Alert (foo instanceof function); False
constructor with function foo
Alert (foo instanceof function);//true


function
First of all, review the concept of the function object, function is the object, representing the function object is the function object. All function objects are constructed from function objects. In other words, the function is the topmost constructor. It constructs all the objects in the system, including user-defined objects, system-built objects, and even its own. This also indicates that the function has a self lift (its own ability to construct itself). This also indirectly determines the same [[call]] and [[constructor]] logic of the function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.