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
本文通过示例介绍了JavaScript中对象和原型的概念,并演示了如何使用原型来实现继承,还展示了如何调用父类的方法。
1050

被折叠的 条评论
为什么被折叠?



