Javascript 中对象如何引用自身?

发布于 2024-09-25 08:58:13 字数 401 浏览 2 评论 0原文

我正在试验 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 技术交流群。

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

发布评论

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

评论(2

涙—继续流 2024-10-02 08:58:13

好吧,实际上两者的作用完全相同:

Dog.prototype = new Animal;

方法内的 this 值将引用调用引用的基对象,在以下情况下:

Dog.inherits(Animal);

< code>this 值将引用 Dog 构造函数,obj 参数将是 Animal 函数。

当您调用时:

Animal.pass(Dog);

this 值将引用 Animal 函数,最后执行与 inherits 方法完全相同的操作,但是反过来说。

我建议您不要扩展Object.prototype对象,因为它可能会导致很多问题,例如这两个属性将在任何中枚举for-in 循环,例如:

for (var prop in {}) { // <-- an empty object!
  alert(prop); // will alert 'inherits' and 'pass'
}

所有对象都继承自 Object.prototype,并且您似乎打算仅在 Function 对象上使用这些方法,扩展 会更安全>Function.prototype 对象,或将方法实现为带有两个参数的函数。

Well, actually the two are doing exactly the same:

Dog.prototype = new Animal;

The this value inside the methods will refer to the base object where the reference was invoked, in the case of:

Dog.inherits(Animal);

The this value will refer to the Dog constructor function, and the obj argument will be the Animal function.

When you call:

Animal.pass(Dog);

The this value will refer to the Animal function, doing at the end exactly the same thing as the inherits 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 any for-in loop, e.g.:

for (var prop in {}) { // <-- an empty object!
  alert(prop); // will alert 'inherits' and 'pass'
}

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 the Function.prototype object, or implement the methods as functions that take two parameters.

無處可尋 2024-10-02 08:58:13

对我有用,测试代码如下:

function Animal() {}
Animal.prototype.isanimal= 1;
function Dog() {}
Animal.pass(Dog);
Dog.prototype.isdog= 2;
alert(new Dog().isanimal); // 1
alert(new Dog().isdog);    // 2

但是,请记住 new thisnew obj 将调用函数 this/obj,创建一个新的完整实例。如果该函数中有构造函数代码期望接收一些参数或设置实例状态,则最终可能会出现问题。要创建新的 this 而不将 this 作为函数调用,您可以使用不执行任何操作的不同构造函数:

function nonconstructor() {}
nonconstructor.prototype= this.prototype;
obj.prototype= new nonconstructor();

此外,您应该避免在 Object 上进行原型设计。这会给使用 Object 作为通用查找映射的代码带来麻烦。由于您似乎只使用构造函数,因此在 Function 上进行原型设计应该可以满足您的需求并且更安全。

Works for me, with test code like:

function Animal() {}
Animal.prototype.isanimal= 1;
function Dog() {}
Animal.pass(Dog);
Dog.prototype.isdog= 2;
alert(new Dog().isanimal); // 1
alert(new Dog().isdog);    // 2

However, bear in mind that new this or new obj will call the function this/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 new this without calling this as a function you can use a different constructor that does nothing:

function nonconstructor() {}
nonconstructor.prototype= this.prototype;
obj.prototype= new nonconstructor();

Also, you should avoid prototyping onto Object. This will cause trouble for code using Object as a general-purpose lookup map. As you only seem to be working with constructor-functions, prototyping onto Function should meet your needs and is much safer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文