<script>//parentfunction Parent(name){this.name = name || ‘Adam‘;}Parent.prototype.say = function(){return this.name;}//childfunction Child(name){}//此处实现继承,需要自己实现inherit(Child, Parent);</script>
//默认模式function inherit(C, P){C.prototype = new P();}var kid = new Child();console.log(kid.say());//输出Adam
<script>//父构造函数function Article(){this.tags = [‘js‘, ‘css‘];}var article = new Article();function BlogPost(){}BlogPost.prototype = article;//获取到对应对象的一个引用var blog = new BlogPost();function StaticPost(){Article.call(this);//获取到对应对象成员的副本,相当于在本对象中生成同样的一个成员变量}var page = new StaticPost();console.log(article.hasOwnProperty(‘tags‘));//trueconsole.log(blog.hasOwnProperty(‘tags‘));//falseconsole.log(page.hasOwnProperty(‘tags‘));//true</script>
<script>function Child(a, b, c, d){Parent.apply(this, arguments);//通过借用获取到父类的成员变量}Child.prototype = new Parent();//通过此处获父类的方法</script>
<script>function Parent(name){this.name = name || "Tom";}Parent.prototype.say = function(){return this.name;}function Child(name){Parent.apply(this, arguments);}Child.prototype = new Parent();var kid = new Child("Child");console.log(kid.name);//childconsole.log(kid.say());//childdelete kid.name;console.log(kid.say());//Tom</script>
function inherit(C, P){C.prototype = P.prototype;}
function inherit(C, P){var F = function(){};F.prototype = P.prototype;C.prototype = new F();}
<script>var klass = function(Parent, props){var Child, F, i;//1. 新构造函数Child = function(){if(Child.uber && Child.uber.hasOwnProperty("_construct")){Child.uber._construct.apply(this, arguments);}if(Child.prototype.hasOwnProperty("_construct")){Child.prototype._construct.apply(this, arguments);}};//2. 继承Parent = Parent || Object;F = function(){};F.prototype = Parent.prototype;Child.prototype = new Child();Child.uber = Parent.prototype;Child.prototype.constructor = Child;//3.添加实现方法for(i in props){if(props.hasOwnProperty(i)){Child.prototype[i]= props[i];}}return Child;};</script>
原文:http://blog.csdn.net/mergades/article/details/41649971