Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的透明分层数据帧_Javascript - Fatal编程技术网

JavaScript中的透明分层数据帧

JavaScript中的透明分层数据帧,javascript,Javascript,对术语没有把握 我有一个包含一些数据的JavaScript“对象”: var parent = { foo: 42, bar: 2.71, baz: 3.14 }; 我需要创建另一个JavaScript对象,该对象将提供对父对象的访问,同时支持对父对象条目的本地覆盖: var child = extend(parent, { foo: 1, quo: 2 }); child.bar = 3; parent.qux = 4; assert(parent.foo === 42); assert

对术语没有把握

我有一个包含一些数据的JavaScript“对象”:

var parent = { foo: 42, bar: 2.71, baz: 3.14 };
我需要创建另一个JavaScript对象,该对象将提供对父对象的访问,同时支持对父对象条目的本地覆盖:

var child = extend(parent, { foo: 1, quo: 2 });

child.bar = 3;
parent.qux = 4;

assert(parent.foo === 42);
assert(child.foo === 1);

assert(parent.bar === 2.71);
assert(child.bar === 3);

assert(parent.baz === 3.14);
assert(child.baz === 3.14);

assert(parent.quo === undefined);
assert(child.quo === 2);

assert(parent.qux === 4);
assert(child.qux === 4);

// NB: I don't care about iteration on these objects.
我需要的解决方案是尽可能低的开销,同时是跨浏览器足够。将父值复制到子值的开销太大

也就是说,我正在寻找类似于Lua链的东西,但是是JavaScript

我在读关于
[[Prototype]]
链的书。看起来这是一条路要走,但我还没有找到一个轻量级的方法来使用它们。(
\uuuuuuuuuuuuuuuuuuuuuu
,据我所知,还不够跨浏览器。)


有什么线索吗?

通过将初始值设定项放到子对象,我可以得出以下结论(由于@FelixKling更新):

是否可以进一步改进

下面是一个JSFIDLE:


请注意,正如注释中所建议的,有一个JS函数,几乎可以完成所需的操作:
对象。创建

听起来像是在寻找
对象。创建
:。很酷,谢谢!是否将其添加为要接受的答案?:)“可以改进吗?”您可以删除所有构造函数内容。我看不出它的目的。
var wrap = function(parent) 
{
  var child = function() { };
  child.prototype = parent;
  return new child();
}