JavaScript prototype of object

来源:互联网 发布:平湖校区网络教学平台 编辑:程序博客网 时间:2024/05/18 00:43


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

0 0
原创粉丝点击