克隆对象和对象的继承

1.克隆对象

通过for in克隆  不管是公有的还是私有的都克隆成私有的

Js提供了一个一个克隆的方法 Object.create()

Var obj2=Object.create(obj)  将obj的所有属性克隆到obj2的原型上

2.对象的继承

// call继承(将父级私有的属性继承为子类的私有属性)
function A(){
this.name="aaa"
}
A.prototype.x=56
function B(){
this.age=23;
A.call(this)
}
B.prototype.y=89;
var a=new A;
var b=new B;
console.log(b)
//原型继承(将父级所有的属性继承为子类的公有属性)
function A(){
this.name="aaa"
}

A.prototype.x=56
function B(){
this.age=23;
}
B.prototype=new A;
var a=new A;
var b=new B;
console.log(b)
//冒充对象继承(将父级的所有属性都继承为子类的私有属性)
//父类
function A(){
this.name="zhangsan"
}
A.prototype.s=56
// 子类
function B(){
// this.age=23;
var temp=new A;
for(var key in temp){
this[key]=temp[key]
}
temp=null
}
var a=new A;
var b=new B;
console.log(b) //将父类所有属性都继承为子类私有的属性
//混合继承(私有的继承私有的,公有的和私有的再次继承为公有的{他是call和原型继承的结合,私有的被继承了两次})
function A(){
this.name="zhangsan"
}
A.prototype.s=56
// 子类
function B(){
this.age=23;
A.call(this)
}
B.prototype=new A;
var a=new A;
var b=new B;
console.log(b)
// 组成继承(私有的继承私有的,共有的继承公有的)
function A(){
this.name="zhangsan"
}
A.prototype.s=56
// 子类
function B(){
this.age=23;
A.call(this)
}
B.prototype=Object.create(A.prototype);
var a=new A;
var b=new B;
console.log(b)
// 中间类继承
function fn(){
console.log(arguments)
arguments.__proto__=Array.prototype;
console.log((arguments))
arguments.shift()
}
fn(12,23,56)
// arguments不是一个数组,没有array的那些自带的方法,现在我们想arguments有那些方法,将arguments 的原型指向array内置的原型

原文地址:https://www.cnblogs.com/mo123/p/10219496.html