标签:style blog io ar color os 使用 sp java
主要使用代码继承来完成复用。<script>
//parent
function Parent(name){
this.name = name || ‘Adam‘;
}
Parent.prototype.say = function(){
return this.name;
}
//child
function 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‘));//true
console.log(blog.hasOwnProperty(‘tags‘));//false
console.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);//child
console.log(kid.say());//child
delete 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>
标签:style blog io ar color os 使用 sp java
原文地址:http://blog.csdn.net/mergades/article/details/41649971