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
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
相关文章推荐
- Object调用控件的办法
- Object详解
- 看啦这么就别人的博客 我也来写一篇! Object转换其他类型
- objective-c感悟(三)属性之谜
- Objective-C中的KVC和KVO
- Objective-C Runtime
- Objective-C 引用计数原理
- Objective-C之run loop详解
- object-c 随机数总结
- warning: could not load any Objective-C class information. This will significantly reduce the qualit
- 通过Object类说明方法重载、方法重写、多态之间的关系
- Objective-C Category和Extension
- iOS 取消延时执行 performSelector: withObject: afterDelay:
- Xcode TODO MARK FIXME 标记
- Object类
- Objective-c的@property 详解
- Objective-C在ARC下结合GCD的单例模式和宏模版
- Tutorial – Creating outline effect around objects
- Object.create
- Object: 多个属性的集合