javascript继承,反射和原型链走? [英] javascript inheritance, reflection and prototype chain walking?

查看:202
本文介绍了javascript继承,反射和原型链走?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚我可以使用的javascript语言本身和多少,我将实现自己,当涉及到对象反射。这里是预期结果

  //属性继承模式(定义默认道具)(存储在dbschemas表中)
foo
bar
moo
baz
ugh

//方法继承模式(存储在代码中)
foo
bar
moo
baz
ugh

// myTree结构+属性覆盖(存储在dbstructs表中)
myBar
myBaz
myUgh
myBar
myBaz
myMoo

的树对象将从myBar结构构造。当构建每个节点时,它将从代码中构造方法,使用继承属性模式和myBar结构本身中定义的节点的任何非默认属性。



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



这是为了允许编辑myBar结构,并指明哪些属性从基础模式(和在哪个级别)继承为默认值,哪些属性在myBar结构中被明确定义。



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



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



谢谢!



所以让我们假设你的继承链是



foo - > bar - > moo。



然后你有一个对象 Foo 这是你的foo节点的原型。



你可以创建一个 Bar



var Bar = Object.create(Foo,props)



现在我们有一个 Bar 原型作为bar节点的原型。



然后对 Moo

执行相同的操作

var Moo = Object.create(Bar,props)



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



然后,您可以简单地获取您知道的任何财产。让我们称之为prop1,并写一个简单的函数,它给你属性所属的对象

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



现在您可能面临的问题之一是原型中的obj chain没有元数据告诉你对象是什么,所以你可能想要添加一个属性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


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!

解决方案

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

这篇关于javascript继承,反射和原型链走?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆