Javascript 中对象如何引用自身?
我正在试验 javascript 中的继承,并编写了这两个函数:
Object.prototype.inherits=function(obj){this.prototype=new obj;}
Object.prototype.pass=function(obj){obj.prototype=new this;}
这段代码运行得很好:
Dog.inherits(Animal);
但以下代码失败了:
Animal.pass(Dog);
据我了解,我的传递函数不起作用,因为“this”不是对该对象的引用实例本身?如果是这样,我如何从对象内部引用该对象?
提前致谢!
I was experimenting with inheritance in javascript, and wrote those two functions:
Object.prototype.inherits=function(obj){this.prototype=new obj;}
Object.prototype.pass=function(obj){obj.prototype=new this;}
This code works very well:
Dog.inherits(Animal);
But the following fails:
Animal.pass(Dog);
As I understand it, my pass functions doesn't work, because "this" isn't a reference to the object instance itself? If that's the case, how can I reference the object from within itself?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,实际上两者的作用完全相同:
方法内的
this
值将引用调用引用的基对象,在以下情况下:< code>this 值将引用
Dog
构造函数,obj
参数将是Animal
函数。当您调用时:
this
值将引用Animal
函数,最后执行与inherits
方法完全相同的操作,但是反过来说。我建议您不要扩展
Object.prototype
对象,因为它可能会导致很多问题,例如这两个属性将在任何中枚举for-in
循环,例如:所有对象都继承自
Object.prototype
,并且您似乎打算仅在 Function 对象上使用这些方法,扩展会更安全>Function.prototype
对象,或将方法实现为带有两个参数的函数。Well, actually the two are doing exactly the same:
The
this
value inside the methods will refer to the base object where the reference was invoked, in the case of:The
this
value will refer to theDog
constructor function, and theobj
argument will be theAnimal
function.When you call:
The
this
value will refer to theAnimal
function, doing at the end exactly the same thing as theinherits
method, but the other way around.I would recommend you to not extend the
Object.prototype
object, because it can cause you a lot of problems, for example those two properties will be enumerated in anyfor-in
loop, e.g.:All objects inherit from
Object.prototype
, and it seems that you intend to use those methods only on Function objects, it would be safer to extend theFunction.prototype
object, or implement the methods as functions that take two parameters.对我有用,测试代码如下:
但是,请记住
new this
或new obj
将调用函数this/obj
,创建一个新的完整实例。如果该函数中有构造函数代码期望接收一些参数或设置实例状态,则最终可能会出现问题。要创建新的this
而不将this
作为函数调用,您可以使用不执行任何操作的不同构造函数:此外,您应该避免在
Object
上进行原型设计。这会给使用Object
作为通用查找映射的代码带来麻烦。由于您似乎只使用构造函数,因此在Function
上进行原型设计应该可以满足您的需求并且更安全。Works for me, with test code like:
However, bear in mind that
new this
ornew obj
will call the functionthis/obj
, creating a new full instance. If you have constructor code in that function that expects to receive some arguments, or sets up instance state, you can end up with problems. To create a newthis
without callingthis
as a function you can use a different constructor that does nothing:Also, you should avoid prototyping onto
Object
. This will cause trouble for code usingObject
as a general-purpose lookup map. As you only seem to be working with constructor-functions, prototyping ontoFunction
should meet your needs and is much safer.