Javascript 获取对包含构造函数属性的对象的引用
这个标题真让人困惑,我找不到更好的了 假设我有:Javascript 获取对包含构造函数属性的对象的引用,javascript,node.js,Javascript,Node.js,这个标题真让人困惑,我找不到更好的了 假设我有: var A = function (){ this.pa = { x: 1 }; }; A.prototype.B = function (){ this.pb = /* a reference to { x: 1 } */; }; var a = new A (); var b = new a.B (); console.log (b.pb.x); //should print 1 a.pa.x = 2; console.l
var A = function (){
this.pa = { x: 1 };
};
A.prototype.B = function (){
this.pb = /* a reference to { x: 1 } */;
};
var a = new A ();
var b = new a.B ();
console.log (b.pb.x); //should print 1
a.pa.x = 2;
console.log (b.pb.x); //should print 2
我想在
pb
中保存对pa
对象的引用。可能吗?嗯,这不完全是我想要的,但非常接近:
var A = function (){
this.pa = { x: 1 };
};
A.prototype.B = function (a){
this.pb = a.pa;
};
var a = new A ();
var b = new a.B(a);
console.log(b.pb.x); //should print 1
a.pa.x = 2;
console.log(b.pb.x);
var A = function (pa){
this.pa = pa;
};
A.prototype.B = function (a){
if (this instanceof A.prototype.B){
if (!a) throw "error";
this.pb = a.pa;
return;
}
return new A.prototype.B (this);
};
var a = new A ({ x: 1 });
var b = a.B ();
console.log (b.pb.x); //1
a.pa.x = 2;
console.log (b.pb.x); //2
new a.B () //throws "error"
这不完全是我想要的,但非常接近:
var A = function (pa){
this.pa = pa;
};
A.prototype.B = function (a){
if (this instanceof A.prototype.B){
if (!a) throw "error";
this.pb = a.pa;
return;
}
return new A.prototype.B (this);
};
var a = new A ({ x: 1 });
var b = a.B ();
console.log (b.pb.x); //1
a.pa.x = 2;
console.log (b.pb.x); //2
new a.B () //throws "error"
一个函数只有一个对新实例的引用,继承自它的原型
要使其保持对原始a
实例的引用,需要将B
构造函数放在闭包中:
function A() {
var that = this;
this.pa = { x: 1 };
this.B = function() {
this.pb = that.pa;
};
};
var a = new A ();
var b = new a.B ();
console.log (b.pb.x); // does print 1
a.pa.x = 2;
console.log (b.pb.x); // does print 2
但是,这样做的缺点是为每个a
实例创建一个新的B
构造函数(具有自己的原型对象)。最好是这样
function A() {
this.pa = { x: 1 };
}
A.B = function() {
this.pb = null;
};
A.prototype.makeB = function() {
var b = new A.B();
b.pb = this.pa;
return b;
};
// you can modify the common A.B.prototype as well
var a = new A ();
var b = a.makeB();
console.log (b.pb.x); // does print 1
a.pa.x = 2;
console.log (b.pb.x); // does print 2
但是,我们可以将这两种方法混合使用,这样您就只有一个原型,但有不同的构造函数:
function A() {
var that = this;
this.pa = { x: 1 };
this.B = function() {
this.pb = that.pa;
};
this.B.prototype = A.Bproto;
}
A.Bproto = {
…
};
一个函数只有一个对新实例的引用,继承自它的原型
要使其保持对原始a
实例的引用,需要将B
构造函数放在闭包中:
function A() {
var that = this;
this.pa = { x: 1 };
this.B = function() {
this.pb = that.pa;
};
};
var a = new A ();
var b = new a.B ();
console.log (b.pb.x); // does print 1
a.pa.x = 2;
console.log (b.pb.x); // does print 2
但是,这样做的缺点是为每个a
实例创建一个新的B
构造函数(具有自己的原型对象)。最好是这样
function A() {
this.pa = { x: 1 };
}
A.B = function() {
this.pb = null;
};
A.prototype.makeB = function() {
var b = new A.B();
b.pb = this.pa;
return b;
};
// you can modify the common A.B.prototype as well
var a = new A ();
var b = a.makeB();
console.log (b.pb.x); // does print 1
a.pa.x = 2;
console.log (b.pb.x); // does print 2
但是,我们可以将这两种方法混合使用,这样您就只有一个原型,但有不同的构造函数:
function A() {
var that = this;
this.pa = { x: 1 };
this.B = function() {
this.pb = that.pa;
};
this.B.prototype = A.Bproto;
}
A.Bproto = {
…
};
new a.B
不会在a.prototype.B
的新实例和a
之间创建关系。构造函数调用与方法调用非常不同,因为它们不接收对方法目标的引用。这就是为什么我要问这个问题。任何获取a
引用的黑客行为都可以使b
成为一个返回绑定货币的getter属性。new a.b
不会在a.prototype.b
的新实例和a
之间创建关系。构造函数调用与方法调用非常不同,因为它们不接收对方法目标的引用。这就是为什么我要问这个问题。任何获取a
引用的黑客行为?您可以使b
成为一个返回绑定货币的getter属性。如果将新a
分配给a
以外的任何对象,则此操作无效。您可以详细说明注释吗?我不明白:)对不起,我忽略了a
是构造函数的一个参数,并且您传入了一些内容。如果您将新a
分配给a
以外的任何对象,这将不起作用。您能详细说明一下注释吗?我不明白:)对不起,我忽略了a
是构造函数的一个参数,并且您传入了一些内容。如果您将newa
分配给a
以外的任何对象,则此操作无效。如果我不理解您的意思,请原谅,但您能否解释一下,如果您将新a分配给a以外的对象,此操作将不起作用。因为虽然您的解决方案更优雅,但您的B不是仍然与A的特定实例相关联吗?哦,对不起,我完全忽略了A
是B
构造函数的一个参数;我假设您试图访问全局a
变量。如果将新a
分配给a
以外的任何对象,则此操作不起作用。如果我不理解您的意思,请您解释一下,如果将新a分配给a以外的对象,此操作将不起作用。因为虽然您的解决方案更优雅,但您的B不是仍然与A的特定实例相关联吗?哦,对不起,我完全忽略了A
是B
构造函数的一个参数;我假设您试图访问全局a
变量。我无法使用第二个选项,因为B
的方法被称为create()
。事实上,B
的真实名称是User
,因此我无法使用名为makeUser()
、createUser()
、newUser()
或newUserInstance()的方法实例化用户;这是一个设计问题。第一个选项确实是我想要实现的,但我必须在a
构造函数中创建B
的原型函数。每次调用新的A
时,就会创建B
的新原型。我可以使用这个解决方案,因为它是有效的,可以工作,但它是无效的,让我回到过去,在我很小的时候,我就开始学习javascript并做这样丑陋的事情。我不能使用第二个选项,因为B
的方法叫做create()
。事实上,B
的真实名称是User
,因此我无法使用名为makeUser()
、createUser()
、newUser()
或newUserInstance()的方法实例化用户;这是一个设计问题。第一个选项确实是我想要实现的,但我必须在a
构造函数中创建B
的原型函数。每次调用新的A
时,就会创建B
的新原型。我可以使用这个解决方案,因为它是有效的,但它是无效的,它让我回到了过去,在我开始学习javascript和做这样丑陋的事情的早期。