如何从子访问父变量
How to access parent variable from child
我试图从B访问a中的变量(在下面的示例中)。我没有从A扩展B,因为A只是一个容器。
function A() {
var Parent = this;
this.container = [];
}
A.prototype.add = function(Item) {
Parent.container.push(Item);
}
function B() {
}
B.prototype.exec = function() {
console.log(Parent.container[0]) //Uncaught ReferenceError: Parent is not defined
}
var Manager = new A();
var Item = new B();
Manager.add(B);
Item.exec();
如何从Item
访问Parent
function A() {
this.container = [];
}
A.prototype.add = function(Item) {
//assigning parent property only if Item is added
Item.Parent = this;
this.container.push(Item);
}
function B() {
this.Parent = null;
}
B.prototype.exec = function() {
console.log(this.Parent.container[0])
}
var Manager = new A();
var Item = new B();
Manager.add(Item);
Item.exec();
function A() {
this.container = [];
}
A.prototype.add = function(Item) {
this.container.push(Item);
}
function B(Parent) {
this.Parent = Parent;
}
B.prototype.exec = function() {
console.log(this.Parent.container[0]) //Uncaught ReferenceError: Parent is not defined
}
var Manager = new A();
var Item = new B(Manager);
A.add(B);
B.exec();
相关文章:
- 如何从字符串变量访问对象属性
- 无法使用变量访问数据 JSON
- D3:使用变量访问绑定数组中的对象
- JavaScript 变量访问错误
- CanvasJS-访问变量-访问属性toString的权限被拒绝
- 如何使用javascript数据对象?如何通过变量访问条目
- 如何使用变量访问对象属性
- 使用变量访问对象的属性
- 如何使用包含对象键名的变量访问对象属性
- 为什么通过缓存变量访问 DOM 更快
- 将 PHP 变量访问到外部 JavaScript 文件中
- 使用变量访问 Javascript 中的对象信息
- 使用变量访问 JSON 属性(字符串)
- 如何在 yii 中从 javascript 变量访问文本字段输入
- 如何使用 javascript 变量访问模型属性
- 根据 HighCharts 对象的变量访问数组的元素
- 使用动态变量访问 Meteor 中另一个变量的值
- 如何通过绑定的控制器变量访问服务的属性
- Angular JS工厂和变量访问
- 使用变量访问对象的值