understanding Prototype in javascript

Possible Duplicate:
How does JavaScript .prototype work?

Coming from Java background, I'm trying to understand javascript.

Please let me know if these are right.

  • Like in java, there is a supreme Object, from which all other objects inherit.
  • The prototype property is like a pointer that points to the parent object (classes in java)
  • For "Object" object, the prototype is null.
  • prototype property's values are strings denoting the objects nomenclature and aren't pointers like in C. The pointer concept is implemented using the hidden attribute,[[PROTOTYPE]] that is not accessible in script.
  • I'm using node.js instead of browser to learn JS. I tried,

    var human = Object.create(null);  // same as var human;
    console.log(Object.getPrototypeOf(human)); //null
    
    var man  = Object.create(human);
    console.log(Object.getPrototypeOf(man));
    //{}
    //expected 'human'
    
    var person = Object.create(Object.prototype); // same as var person = {}
    console.log(Object.getPrototypeOf(person));
    //{}
    //expected 'object'
    

  • Yes, with the exception of Object.create(null) which doesn't inherit from anything.
  • Not sure what "parent object" is, but the prototype of a function consists of functions that are accessible by instances. Because Object.prototype.toString exists, so does {}.toString - it is inherited.
  • Yes, the prototype chain ends here.
  • No, .prototype (or the value returned by Object.getPrototypeOf ) is an object which you can extend with properties, so that those properties are inherited by instances.
  • Your examples:

    var human = Object.create(null);  // same as var human;
    

    No, it creates an empty object that doesn't inherit from anything. var human; sets human to undefined - which is not an object but a primitive value (not everything is an object in JavaScript).

    var man  = Object.create(human);
    console.log(Object.getPrototypeOf(man));
    //{}
    //expected 'human'
    

    Object.getPrototypeOf(man) returns the object human . This is an empty object; node shows it as {} . It is not a string. In fact, objects can be assigned to multiple variables. An object does not have a variable bound to it, so by design it is not possible at all to get a string. To check equality, you could do Object.getPrototypeOf(man) === human which yields true .

    var person = Object.create(Object.prototype); // same as var person = {}
    console.log(Object.getPrototypeOf(person));
    //{}
    //expected 'object'
    

    This is indeed the same as {} - an empty object that inherits directly from Object . As said above, the prototype is Object.prototype and not a string. It looks empty but that's because Object.prototype 's functions are available by default and hidden.


    First, you need to read about the create method before you attempt to use it.

    Second, invoke objects in the following manner:

    var human = {}, man = {}, person = {};
    

    Third, this is not Java. In Java classes and an understanding of classes are pretty important. Prototypes are completely optional in JavaScript. The only performance benefit offered by prototypes comes when you code makes heavy reuse of functions by reference in different scopes. If this style of coding does not apply to you then you likely do not need to use prototypes.

    Prototypes are inherited from the parent, or some ancestor, object. A JavaScript parser will first look for local assignments and if none exist will then examine the current object's prototype for the presence of the named reference. In a nutshell this all you need to know at this early point in writing JavaScript.


    Firstly, IMO, don't start off with node. Node is going to drag you into a pool of quicksand if you don't know javascript yet. Secondly, when I started off with function based code, then OO and only then I moved to prototypes. But if you really want to know, this question has already been answered over here. Feel free to ask me if you have any more questions!

    链接地址: http://www.djcxy.com/p/30080.html

    上一篇: 函数对象原型

    下一篇: 了解JavaScript中的原型