简体   繁体   English

在对象中定义因变量的最佳方法是什么?

[英]What is the best way to define dependent variables in an object?

In the Google developers recommendation for optimizing JavaScript code, they mention that the best way to declare/initialize new variables for object is to use the prototype. Google开发人员关于优化JavaScript代码的建议中,他们提到为对象声明/初始化新变量的最佳方法是使用原型。 For instance, instead of: 例如,代替:

foo.Bar = function() {
this.prop1_ = 4;
this.prop2_ = true;
this.prop3_ = [];
this.prop4_ = 'blah';
};

Use: 采用:

foo.Bar = function() {
this.prop3_ = [];
};

foo.Bar.prototype.prop1_ = 4;
foo.Bar.prototype.prop2_ = true;
foo.Bar.prototype.prop4_ = 'blah';

However, in my case I have a dependency between variables, for instance: 但是,就我而言,我在变量之间有一个依赖关系,例如:

var appv2 = function(){
this.start(this.person, this.car); 
}; 

appv2.prototype.toWhom = 'Mohamed'; 
appv2.prototype.person = new person(this.toWhom); 
appv2.prototype.car = new car();

appv2.prototype.start = function(person, car){
console.log('start for appv2 is called'); 
person.sayHello('me app v2');
car.brand();    
}; 

new appv2(); 

Using this.toWhom outside of the main constructor body or a method function of the object will yield undefined. 在主要构造函数主体或对象的方法函数之外使用this.toWhom将产生未定义的结果。 To solve this I could use appv2.prototype.toWhom instead of this.toWhom or I could declare my dependent variables inside of the main constructor body. 为了解决这个问题,我可以使用appv2.prototype.toWhom而不是this.toWhom,或者可以在主构造函数主体中声明我的因变量。

But I would like to know what is the best way, in terms of performance, to accomplish this? 但是我想知道在性能方面最好的方法是什么?

Thanks 谢谢

To reference toWhom while creating person , you can either store the value in a separate variable: 要在创建person引用toWhom ,可以将值存储在单独的变量中:

var toWhom = appv2.prototype.toWhom = 'Mohamed';
appv2.prototype.person = new person(toWhom);

Or, reference it from the prototype , as you suspected: 或者,您怀疑从prototype引用了它:

appv2.prototype.person = new person(appv2.prototype.toWhom);

The reason this.toWhom is undefined is because this doesn't refer to an instance of appv2 there. this.toWhom之所以undefined是因为this没有引用appv2的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用方法和属性定义javascript对象的最佳方法是什么? (如果可能以本机方式) - What is the best way to define a javascript object with methods and properties? (if its possible in a native way) 在Angular控制器中定义常数的最佳方法是什么? - What is the best way to define a constant in an Angular controller? 在 React 中,定义动作类型的最佳方式是什么? - In React, what is the best way to define action types? 加载依赖于页面的面向对象JS属性的最佳方法是什么? - Whats the best way to load Object Oriented JS properties dependent on page? 构造此对象的最佳方法是什么? - What is the best way to construct this object? 定义键是变量的javascript关联数组的最佳方法? - Best way to define javascript associative array where keys are variables? 检查多个变量是否已设置的最佳方法是什么? - What is the best way to check that multiple variables are set? 为网络流源/接收器定义逻辑的最佳方法是什么? - What is the best way to define logic for web-streams sources/sinks? 在javascript函数范围内定义变量的最佳方法是什么? - What's the best way to define a variable within the scope of a javascript function? 如果未定义数组单元格,最好的方法是什么? - what is the best way define array cell if it's not defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM