Sunday 15 January 2012

javascript - Isn't a prototype object an instance? -


I have some code that can not understand. I did not understand why the false returned.
Is not a prototype object an example?

  js> "" .__proto (new string ("")) jsa & gt; (New string ("")) example string true js & gt; ".__. Proto__ instanceof string false    

This is basically the same.

string.prototype is , according to a string, specification:

string prototype object is the string object <<>> ] The value of the internal property is the standard built object prototype object ().

But this constructor function is not an example of the string .

Property constructor function of prototype by comparing the prototype of this example, i.e.

  object.jetprototypeoff (string.prototype) == = String.prototype   

Therefore, you are checking whether string.prototype is the self prototype, which is exactly False is . Prototype of String.prototype , as specified in the specification, Object.prototype .


One word about the first example:

  & gt; JS & gt; "" .__proto (new string (""))   

You have to be careful that the console (or RELP) uses some estimates to format and present values, for example For this object, the "code" creator can read the property, to determine what type of "value" is and according to which the output is formatted what the Chrome console is doing:

  var str = new string ('foo'); Str instanceof string = & gt; True console Dior (str); = & Gt; String Vor obj = {}; Console.dir (obj); = & Gt; Object // good obj.constructor = string appears; = & Gt; Console.dir (obj); String // what is yaham? Obj instanceof string = & gt; False   

It happens that every prototype object has a constructor property, which points back to the function, it is a prototype object, that is,

  function fu () {} Foo.prototype.constructor === Foo; = & Gt; True Console Der (F.Portotype) = & gt; Foo   

So if you log a prototype object, then the output can suggest that the object is an example of the function, even though it is not, only constructor Property

No comments:

Post a Comment