gpt4 book ai didi

Javascript:从已经实例化的对象与原型(prototype)创建对象

转载 作者:行者123 更新时间:2023-11-29 10:18:22 24 4
gpt4 key购买 nike

我有一个相当学术的问题,并不特别适用于我正在做的任何事情,我真的很想知道答案!

假设我们在全局命名空间中有一个简单的对象定义:

TestObject = function(){};

它有一个添加到它的原型(prototype)的方法,可以实例化到一个新对象本身:

TestObject.prototype.AnotherObject = function() {};

实例化第一个对象:

var myObject = new TestObject();

现在我的问题是:

如何

myObject.myProperty = new myObject.AnotherObject();

不同于

myObject.myProperty = new TestObject.prototype.AnotherObject();

或者它们完全一样?

我看到的区别是:我可以使用第二种方法在 TestObject 上下文中实例化对象,而无需知道实例化对象本身的名称,即

TestObject.prototype.createAnObject = function() {
this.anotherProperty = new TestObject.prototype.AnotherObject();
}

最后:

使用原型(prototype)方法实例化同名对象有什么含义?为什么这会导致无限循环? (里面到底发生了什么……)

TestObject.prototype.AnotherObject = function () {
this.AnotherObject = new TestObject.prototype.AnotherObject();
};
myObject.AnotherObject();

然而这并不...

TestObject.AnotherObject = function() {};

TestObject.prototype.createAnObject = function() {
this.AnotherObject = new TestObject.prototype.AnotherObject();
};
myObject.createAnObject();

...

我很想了解这里的对象之间的关系!谢谢!

我问这些问题的原因是因为我想在对象之间有 1:1 关系的地方做这样的事情:

ClientObject = function () {
this.objectname = "a client class";
}

ClientObject.prototype.loginUser = function(name) {
this.loggedin = true;
if (typeof this.User === 'undefined') {
this.User = new ClientObject.User(name);
}
}
ClientObject.User = function (name) {
this.username = name;
}

ClientObject.User.prototype.getProfile = function() {
return 'user profile';
}

var testClient = new ClientObject();

console.log('testClient.User = ' + (typeof testClient.User)); // should not exist
testClient.loginUser('Bob'); // should login 'bob'
console.log('testClient.User = ' + (typeof testClient.User)); // should exist now Bob is logged in
console.log(testClient.User.username); // should be bob
testClient.loginUser('Tom'); // should not do anything as User object already created
console.log(testClient.User.username); // bob still
console.log(testClient.User.getProfile()); // new functionality available

我只是不确定我是否无意中违反了这里的任何最佳实践或约定。

最佳答案

myObject.myProperty = new myObject.AnotherObject();

differ to

myObject.myProperty = new TestObject.prototype.AnotherObject();

完全没有区别。请记住,JavaScript 中的对象有一个原型(prototype)链。当您调用 new myObject.AnotherObject(); 时,引擎将首先检查 myObject 本身是否有 AnotherObject。如果找不到它,它将检查它会找到的 myObject 的原型(prototype)。第二个版本

myObject.myProperty = new TestObject.prototype.AnotherObject();

直接转到定义AnotherObject的地方。


TestObject.prototype.AnotherObject = function () {
this.AnotherObject = new TestObject.prototype.AnotherObject();
}
myObject.AnotherObject();

只需遍历代码。当您说:myObject.AnotherObject(); 时,将调用 AnotherObject,并将 this 设置为 myObject。第一行将尝试在 myObject(即 this)上创建一个新属性,方法是将其设置为

的结果
new TestObject.prototype.AnotherObject(); 

然后将重新进入完全相同的 AnotherObject 函数,但这次将 this 设置为一个新对象,其原型(prototype)设置为 TestObject.prototype .AnotherObject 的原型(prototype)。等等无穷无尽


最后,

TestObject.prototype.createAnObject = function() {
this.AnotherObject = new TestObject.prototype.AnotherObject();
}
myObject.createAnObject();

不会导致无限循环,据我所知,据我测试:FIDDLE

基本上,createAnObject 将在 this 设置为 myObject 的情况下进入。在其中,将在 myObject 上创建一个名为 AnotherObject 的全新属性,该属性将设置为您之前对 AnotherObject function 的新调用设置。

请注意,在进行此调用后,AnotherObject 函数仍然存在但是,它将被您刚刚创建的AnotherObject属性遮蔽。所以现在你永远无法说

var f = new myObject.AnotherObject()

因为您现在有一个 AnotherObject property 就在 myObject 上,它会在检查原型(prototype)上的任何内容之前被发现并返回。

好吧,我的意思是,你总是可以说 delete myObject.AnotherObject 并从对象中删除该属性,这样你就可以看到 AnotherObject原型(prototype),但实际上,您应该首先避免这样的名称冲突。


关于你的最后一段代码

A) 为什么不让 User 成为它的自己的函数?
B) 为什么不在 ClientObject 构造函数中设置 this.User = new ...()?这样你就不需要 undefined 检查C) ClientObject 应该定义为

function ClientObject(){...` 

您现在拥有的似乎是在创建一个隐式全局。

关于Javascript:从已经实例化的对象与原型(prototype)创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16889166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com