创建一个独立的Javascript对象,该对象从父对象继承而不是从子对象继承到父对象

创建一个独立的Javascript对象,该对象从父对象继承而不是从子对象继承到父对象,javascript,inheritance,Javascript,Inheritance,我的目标是让otherFoo从foo继承,但不要继承回父对象。我尝试了使用Object.create的几个角度,但它一直继承回父对象 var foo = { "bar": {"data1":"02", "data2":"03"}, "otherBar":"09"}; var otherFoo = {}; otherFoo.bar = foo.bar; otherFoo.bar.data1 = "04"; 我希望最终结果是 // foo = { "bar": {"data1":"02", "d

我的目标是让otherFoo从foo继承,但不要继承回父对象。我尝试了使用Object.create的几个角度,但它一直继承回父对象

var foo = { "bar": {"data1":"02", "data2":"03"}, "otherBar":"09"};

var otherFoo = {};
otherFoo.bar = foo.bar;
otherFoo.bar.data1 = "04";
我希望最终结果是

// foo = { "bar": {"data1":"02", "data2":"03"}};
// otherFoo = { "bar": {"data1":"04", "data2":"03"}};
这是一张方便的便条,你是说

let foo={“bar”:{“data1”:“02”,“data2”:“03”},otherBar”:“09”};
让otherFoo=Object.assign({},foo);
foo.otherBar=10;
console.log(foo);
console.log(otherFoo)你的意思是

let foo={“bar”:{“data1”:“02”,“data2”:“03”},otherBar”:“09”};
让otherFoo=Object.assign({},foo);
foo.otherBar=10;
console.log(foo);

console.log(otherFoo)您可以尝试使用类:

function template(data1) {
    this.data = { "bar": {"data1": typeof data1 !== 'undefined' ? data1: '02', "data2":"03"}};
}
var foo = new template();
var otherFoo = new template('04');
console.log(foo.data);
console.log(otherFoo.data);

您可以尝试使用一个类:

function template(data1) {
    this.data = { "bar": {"data1": typeof data1 !== 'undefined' ? data1: '02', "data2":"03"}};
}
var foo = new template();
var otherFoo = new template('04');
console.log(foo.data);
console.log(otherFoo.data);

如果可以使用对象扩展操作符:

var foo = { "bar": {"data1":"02", "data2":"03"}, "otherBar":"09"};

var { otherBar, ...bar } = foo
foo = bar
var fooBar = {
    bar: { ...foo.bar, data2: "04" }
}

否则,您需要对象的深度克隆。

如果可以使用对象扩展操作符:

var foo = { "bar": {"data1":"02", "data2":"03"}, "otherBar":"09"};

var { otherBar, ...bar } = foo
foo = bar
var fooBar = {
    bar: { ...foo.bar, data2: "04" }
}

否则,您需要对象的深度克隆。

接近Talha的答案,但我想我还是继续发布它(因为它几乎完全模仿了您上面的示例,您希望数据以什么样的形式表示)

const bar=函数(数据){
this.bar=数据;
}
设foo=newbar({“data1”:“02”,“data2”:“03”})
让otherFoo=newbar({“data1”:“04”,“data2”:“03”})
console.log(foo)

log(otherFoo)
接近Talha的答案,但我想我还是继续发布它(因为它几乎完全模仿了上面的示例,说明了您希望数据以什么样的形式表示)

const bar=函数(数据){
this.bar=数据;
}
设foo=newbar({“data1”:“02”,“data2”:“03”})
让otherFoo=newbar({“data1”:“04”,“data2”:“03”})
console.log(foo)

log(otherFoo)
这里有一个函数,可以深度克隆任何对象,包括循环对象。它还保持原始对象中的循环引用,不引用原始对象中的任何值,除了
原型
链中的值:

函数克隆(object,objects=new WeakMap()){
//这将忽略复制原语
if(对象的类型!='object'&&typeof对象!=='function'|| object==null){
返回对象;
}
if(objects.has(object)){
返回objects.get(object);
}
const proto=Object.getPrototypeOf(对象);
常量描述符=Object.getOwnPropertyDescriptors(对象);
const copy=typeof对象==“函数”
?object.call.bind(对象)
:Object.create(proto);
对象。设置(对象,副本);
Object.values(描述符).forEach(描述符=>{
if(描述符中的“值”){
descriptor.value=克隆(descriptor.value,对象);
}
});
返回对象。定义属性(副本、描述符);
}
设foo={bar:{data1:'02',data2:'03'},otherBar:'09'};
课堂测试{
构造函数(…args){this.args=args;}
cool(){return'feature';}
}
foo.bar.foo=foo;
foo.bar.bar=foo.bar;
foo.test=新测试('foo','bar');
foo.bar.test=foo.test;
foo.bar.func=()=>“func”;
console.log(foo);
var otherFoo=克隆(foo);
//一切照常进行
console.log(otherFoo);
log(otherFoo.test instanceof test);
console.log(otherFoo.bar.func()='func');
console.log(typeof otherFoo.bar.func=='function');
//没有任何内容引用原始对象
console.log(foo!==otherFoo);
console.log(foo.bar!==otherFoo.bar);
console.log(foo.test!==otherFoo.test);
console.log(foo.bar.func!==otherFoo.bar.func);
//不复制原型
log(foo.test.cool==otherFoo.test.cool)

.as控制台包装{max height:100%!important}
这里有一个函数,可以深度克隆任何对象,包括循环对象。它还保持原始对象中的循环引用,不引用原始对象中的任何值,除了
原型
链中的值:

函数克隆(object,objects=new WeakMap()){
//这将忽略复制原语
if(对象的类型!='object'&&typeof对象!=='function'|| object==null){
返回对象;
}
if(objects.has(object)){
返回objects.get(object);
}
const proto=Object.getPrototypeOf(对象);
常量描述符=Object.getOwnPropertyDescriptors(对象);
const copy=typeof对象==“函数”
?object.call.bind(对象)
:Object.create(proto);
对象。设置(对象,副本);
Object.values(描述符).forEach(描述符=>{
if(描述符中的“值”){
descriptor.value=克隆(descriptor.value,对象);
}
});
返回对象。定义属性(副本、描述符);
}
设foo={bar:{data1:'02',data2:'03'},otherBar:'09'};
课堂测试{
构造函数(…args){this.args=args;}
cool(){return'feature';}
}
foo.bar.foo=foo;
foo.bar.bar=foo.bar;
foo.test=新测试('foo','bar');
foo.bar.test=foo.test;
foo.bar.func=()=>“func”;
console.log(foo);
var otherFoo=克隆(foo);
//一切照常进行
console.log(otherFoo);
log(otherFoo.test instanceof test);
console.log(otherFoo.bar.func()='func');
console.log(typeof otherFoo.bar.func=='function');
//没有任何内容引用原始对象
console.log(foo!==otherFoo);
console.log(foo.bar!==otherFoo.bar);
console.log(foo.test!==otherFoo.test);
console.log(foo.bar.func!==otherFoo.bar.func);
//不复制原型
log(foo.test.cool==otherFoo.test.cool)

.as console wrapper{max height:100%!important}
你是说“复制”对象?我刚刚更新了它,我打算只复制
bar
而不是
otherBar
参考人。参考资料。我很懒,有人能找到这个问题的副本吗?实现同样目标的另一种方法:继承?你的意思是像
otherFoo=Object.create(foo);otherFoo.bar=Object.create(foo.bar)?看一看你的意思是“复制”对象?我刚刚更新了它,我打算只复制
bar
而不是
otherBar