简体   繁体   English

在不创建新实例的情况下访问构造函数的属性

[英]Accessing property of constructor without creating new instance

Is it possible to access properties of a constructor object/function without first creating an instance from it ? 是否可以在不先从中创建实例的情况下访问构造函数对象/函数的属性?

For example, let's say I have this constructor: 例如,假设我有这个构造函数:

function Cat() {
  this.legs = 4;
};

And now – without creating a new cat instance – I want to know what value of legs is inside the constructor. 现在 - 没有创建一个新的cat实例 - 我想知道构造函数中的legs值是什么。 Is this possible? 这可能吗?

(And I'm not looking for stuff like: var legs = new Cat().legs . (Let's say the instantiation of a new Cat is super CPU expensive for some reason.)) (而且我不是在寻找像这样的东西: var legs = new Cat().legs 。(假设新Cat的实例化由于某些原因而超级CPU昂贵。))

Does something like this count? 这样的事情算什么?

function Cat() {
  this.legs = 4;
}

var obj = {};
Cat.call(obj);
console.log(obj.legs); // 4

In your scenario, leg is an instance variable, which means that an object instance is needed in order to access it. 在您的场景中, leg是一个实例变量,这意味着需要一个对象实例才能访问它。

You can make it a pseudo- class variable (see class variable in Javascript ), you should be able then to access it without calling the function (instantiating the object). 你可以把它变成一个伪class variable (参见Javascript中的类变量 ),然后你应该能够在不调用函数的情况下访问它(实例化对象)。

这甚至更贵:

console.log(parseInt(Cat.toString().match(/this\.legs\s*=\s*(\d+)/)[1]));

There's a hundred ways to do this, but the static default pattern below is as good as any of them: 有一百种方法可以做到这一点,但下面的静态默认模式与其中任何一种一样好:

function Cat(opts) {
  var options = opts || {};
  this.legs == options.legs || Cat.defaults.legs;
};

Cat.defaults = {
  legs: 4
}

var myCat = new Cat({legs:3}); //poor guy
var normalNumberOfCatLegs = Cat.defaults.legs;

In short, no you can't access that variable without creating an instance of it, but you can work around it: 简而言之,如果没有创建它的实例,你就无法访问该变量,但是你可以解决它:

Global Variable 全局变量

Assuming 'legs' may vary as the application runs, you may want to create a "global" variable by assigning legs to 'window', an object that will exist throughout the life of the page: 假设“leg”可能随着应用程序的运行而变化,您可能希望通过将“leg”指定为“window”来创建“全局”变量,这是一个在整个页面生命周期中都存在的对象:

window.legs = 4; // put this at the top of your script

Then throughout the course of the application, you can change this until you're ready to use it in Cats(): 然后在整个应用程序的过程中,您可以更改此内容,直到您准备好在Cats()中使用它:

window.legs = user_input;

Global Object 全球对象

You could even assign an object to window, if you want Cats to have other, alterable attributes: 如果您希望Cats具有其他可变属性,您甚至可以将对象分配给窗口:

window.Cat = {};
window.Cat.legs = 4;
window.Cat.has_tail = true;
window.Cat.breeds = ["siamese","other cat breed"];

How to use Globals 如何使用Globals

Then, when a Cat object is created later on, you can pull the data from window to create an instance of Cat: 然后,当稍后创建Cat对象时,您可以从窗口中提取数据以创建Cat的实例:

function Cat(){
  this.legs = window.legs;
  //or
  this.legs = window.Cat.legs;
}
// cat.js
function Cat() {

}

Cat.prototype.legs = 4;

// somescript.js
var legs = Cat.prototype.legs
var cat = new Cat();
console.log(legs, cat.legs); // 4, 4

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM