首页 > 编程语言 > 详细

JavaScript Tips: 原型链 prototype

时间:2017-02-08 18:29:21      阅读:176      评论:0      收藏:0      [点我收藏+]

 

当Bar.prototype = new Foo();

$(function () {
    var test = new Bar();
    console.log(test);//Bar
    test.method();//method log
    console.log(test.foo);//Hello World
    console.log(test.value);//42
    var footest = new Foo();
    console.log(footest);//Foo
    console.log(footest.foo);//undefined
    console.log(footest.value);//42
});
    
function Foo(){
    this.value=42;
    }
    Foo.prototype = {
    method:function(){console.log(‘method log‘)}
};
function Bar(){}
Bar.prototype = new Foo();
Bar.prototype.foo = ‘Hello world‘;
Bar.prototype.constructor=Bar;

当Bar.prototype=Foo.prototype;

$(function () {
    var test = new Bar();
    console.log(test);//Bar
    test.method();//method log
    console.log(test.foo);//Hello World
    console.log(test.value);//undefined
    var footest = new Foo();
    console.log(footest);//Foo
    console.log(footest.foo);//Hello World
    console.log(footest.value);//42
});
    
function Foo(){
    this.value=42;
    }
    Foo.prototype = {
    method:function(){console.log(‘method log‘)}
};
function Bar(){}
Bar.prototype = Foo.prototype;
Bar.prototype.foo = ‘Hello world‘;
Bar.prototype.constructor=Bar;

 

原理:补充。

 

JavaScript Tips: 原型链 prototype

原文:http://www.cnblogs.com/jane850113/p/6379072.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!