What does 'is an instance of' mean in Javascript?

后端 未结 7 788
面向向阳花
面向向阳花 2020-12-18 00:42

The answer to this question: What is the initial value of a JavaScript function's prototype property?

has this sentence:

The initial value

7条回答
  •  粉色の甜心
    2020-12-18 01:09

    Javascript doesn't use classical inheritance, it uses prototypal inheritance where everything is an "Object" and inheritance is accomplished by cloning, for example:

    var myPrototype = function() {
       var privateMember = "I am private";
       var publicMember = "I am public";
       var publicMethod = function () {
           alert(privateMember); 
       }
       return {
          publicMember = publicMember,
          publicMethod = publicMethod
       }
    }
    
    var myInstace = new myPrototype();
    myInstance.publicMethod();
    

    here myInstance can be said to be an "instance of" myPrototype, but in reality what happened is by using the new keyword you cloned myPrototype to create myInstance.

提交回复
热议问题