请问obj2对象深拷贝一个obj1的对象直接量,算不算obj2对象继承了obj1对象呢
这样obj2能不能说继承了obj1? 代码如下:
var obj1={a:1,method:function(){return 123}}var obj2={}for (q in obj1){ obj2[q]=obj1[q]}alert(obj2.method())
//1.不是继承,是克隆//2.继承的唯一写法 function A() {this.name1 = "a";}A.prototype.getName1 = function(){return this.name1;}function B(){this.name2 = "b";}B.prototype = new A();//通过prototype继承B.prototype.constructor = B;//new B()后 对象的构造方法是自己BB.prototype.getName2 = function(){return this.name2;}var b = new B();alert(b.getName1())alert(b.getName2())//3.克隆功能的可以写个公共方法 Object.prototype.clone = function(){ //具体业务}var obj = new B();obj.name2 = "2b";alert(obj.clone().getName2());