javascript继承、反射和原型链行走?

发布于 2024-12-09 05:06:48 字数 805 浏览 0 评论 0原文

我试图弄清楚我可以在多大程度上使用 javascript 语言本身,以及在涉及对象反射时我需要自己实现多少。这是预期的结果,

// property inheritance schema (defining default props) (stored in db "schemas" table)
foo
    bar
        moo
    baz
        ugh

// method inheritance schema (stored in code)
foo
    bar
        moo
    baz
        ugh

// myTree structure + property overrides (stored in db "structs" table)
myBar
    myBaz
    myUgh
        myBar
    myBaz
        myMoo

将从 myBar 结构构造 Tree 对象的实例。在构建每个节点时,它将使用代码中的方法以及 myBar 结构本身中定义的节点的“继承”属性模式和任何非默认属性来组合方法。

目标是创建一个系统,给定来自 Tree 实例的节点,可以自我分析其自己的属性继承路径并识别属性在哪个级别定义。

这是为了允许编辑 myBar 结构,并指示哪些属性是从基本架构(以及在哪个级别)继承为默认值,以及哪些属性是在 myBar 结构中显式定义的。

问题是,使用 JS 递归分析 .constructor 和 .prototype 属性可以确定多少,以及需要自定义实现多少?

我不确定这是否足够清楚,所以请要求澄清。

谢谢!

i'm trying to figure out how much i can use of the javascript language itself and how much i would have to implement myself when it comes to object reflection. here's the intended result

// property inheritance schema (defining default props) (stored in db "schemas" table)
foo
    bar
        moo
    baz
        ugh

// method inheritance schema (stored in code)
foo
    bar
        moo
    baz
        ugh

// myTree structure + property overrides (stored in db "structs" table)
myBar
    myBaz
    myUgh
        myBar
    myBaz
        myMoo

an instance of a Tree object will be constructed from the myBar structure. when building each node, it will compose the methods from the code, with the "inherited" property schema and any non-default properties from the node defined in the myBar struct itself.

the goal is to create a a system that, given a node from an instance of Tree, can self-analyze its own property inheritance path and identify at which level the property is defined.

this is to allow editing of the myBar structure and indicate which properties are inherited as defaults from the base schema (and at which level) and which ones are explicitly defined in the myBar structure.

the question is, how much can be determined from recursively analyzing the .constructor and .prototype properties using JS, and how much would need to be custom implemented?

i'm not sure if this is sufficiently clear, so please ask for any clarification.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-12-16 05:06:49

您想要构建原型链。

假设您的继承链是

foo ->酒吧->哞。

然后您就有了一个对象 Foo,它是 foo 节点的原型。

您只需将 Foo 注入其原型链即可创建一个 Bar 对象。

var Bar = Object.create(Foo, props)

现在我们有了一个 Bar 原型,它是 bar 节点的原型。

然后对 Moo 执行相同的操作

var Moo = Object.create(Bar, props)

现在假设您有一个 moo 节点。

然后你可以简单地拿走任何你知道的财产。让我们将其称为“prop1”并编写一个简单的函数,该函数为您提供属性所属的对象

var findPropertyOwner = function(obj, prop) {
  do {
    if (obj.hasOwnProperty(prop)) {
      return obj;
    }
  } while (obj = Object.getPrototypeOf(obj));
}

现在您可能面临的问题之一是原型链中的 obj 没有元数据告诉您该对象是什么,因此您可以想要向所有节点原型对象添加属性“name”,以便您可以更轻松地检查它是什么。

您可能还希望让 findPropertyOwner 返回一个 (obj, count) 元组,如下所示。

var findPropertyOwner = function(obj, prop) {
  var i = 0;
  do {
    if (obj.hasOwnProperty(prop)) {
      return [obj, i];
    }
  } while (i++, obj = Object.getPrototypeOf(obj));
}

这样您就可以获得更多信息,例如在原型链中找到该属性的位置。另请注意,当 do/while 循环终止时 (Object.getPrototypeOf(Object.prototype) === null) 它将返回 undefined

You want to build prototype chains.

So let's say your chain of inheritance is

foo -> bar -> moo.

Then you have an object Foo that is your prototype for foo nodes.

You can create a Bar object by simply injecting Foo into it's prototype chain.

var Bar = Object.create(Foo, props)

Now we have a Bar prototype that is the prototype for bar nodes.

Then you do the same for Moo

var Moo = Object.create(Bar, props)

Now let's say you have a moo node.

Then you can simply take any property that you know. Let's call it "prop1" and write a simple function which gives you the object the property belongs to

var findPropertyOwner = function(obj, prop) {
  do {
    if (obj.hasOwnProperty(prop)) {
      return obj;
    }
  } while (obj = Object.getPrototypeOf(obj));
}

Now one of the issues that you may face is that the obj in the prototype chain has no meta data telling you what the object is so you may want to add a property "name" to all your node prototype objects so that you can more easily check what it is.

You may also want to have findPropertyOwner return a tuple of (obj, count) as follows

var findPropertyOwner = function(obj, prop) {
  var i = 0;
  do {
    if (obj.hasOwnProperty(prop)) {
      return [obj, i];
    }
  } while (i++, obj = Object.getPrototypeOf(obj));
}

This way you have more information like how far up the prototype chain that property was found. Also note that when the do/while loop terminates (Object.getPrototypeOf(Object.prototype) === null) it will return undefined

久光 2024-12-16 05:06:49

雷诺斯的回答很棒。我必须稍微调整它,以便它适用于不是从 Object 继承的对象。

var findPropertyOwner = function(obj, prop) {
  var i = 0;
  do {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
      return [obj, i];
    }
  } while (i++, obj = Object.getPrototypeOf(obj));
}

Raynos's answer is great. I had to tweak it slightly, so that it worked for objects not descended from Object.

var findPropertyOwner = function(obj, prop) {
  var i = 0;
  do {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
      return [obj, i];
    }
  } while (i++, obj = Object.getPrototypeOf(obj));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文