您的位置:首页 > 移动开发 > Objective-C

JavaScript prototype of object

2016-03-15 18:42 429 查看

Every JavaScript object has a second JavaScript object (or  null , but this is rare) associated with it. This second object is known as a prototype, and the first object inherits properties from the prototype.so, if we want to
know about prototype, firstly we should know about object.object, it is the foudamental datatype of JvaScript.any value in JavaScript that is not a string, a number,  true ,  false ,  null , or  undefined is an object.

now, let us turn to the basic prototype of Object. the following example is easy appliction of prototype.

function baseClass()

{

  this.showMsg = function()

  {

     alert("baseClass::showMsg");  

  }

}

function extendClass()

{

}

extendClass.prototype = new baseClass();

var instance = new extendClass();

instance.showMsg(); // 显示baseClass::showMsg

In this exampel,the result shows function extendClass() using the property of baseClass .

function baseClass()

{

    this.showMsg = function()

    {

        alert("baseClass::showMsg");  

    }

}

function extendClass()

{

    this.showMsg =function ()

    {

        alert("extendClass::showMsg");

    }

}

extendClass.prototype = new baseClass();

var instance = new extendClass();

instance.showMsg();//显示extendClass::showMsg

however,this program runs is not like the last example.so ,we can see ,the propotype in some degree,just as the variable.when local variable is not exist,it can find the closey related variable.so if we want to use the instance
of extendClass to call the function of baseClass. How can we do?

Do not worry .there is the solution below(use call funtion).

extendClass.prototype = new baseClass();

var instance = new extendClass();

var baseinstance = new baseClass();

baseinstance.showMsg.call(instance);//显示baseClass::showMsg
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: