简体   繁体   English

JavaScript访问对象的未知属性

[英]JavaScript access unknown properties of object

Situation: 情况:

I have multiple JS-Objects with the same structure. 我有多个具有相同结构的JS对象。

In all of those Objects there's a property called console. 在所有这些对象中,都有一个称为console的属性。

I'd like to assign a value (the same value) to each of these properties. 我想为每个属性分配一个值(相同的值)。

Please find below the structure of my code: 请在下面找到我的代码的结构

NameOfMyObject = function() {

     return {

        actions: null,

        console: null,

        init: function() {}, 

        //object specific functions

    }
};

My Problem: 我的问题:

Currently I'm assigning the value manual, but I want do do it generic. 目前,我正在分配值手册,但我想这样做是通用的。

Code snippet 程式码片段

this.console = console;
this.actions.console = console;
this.fixtures.console = console;

How can I access the properties of my objects? 如何访问对象的属性?

I hope I asked my question clear enough. 我希望我的问题足够清楚。

Here is how you would share a property across objects: 这是跨对象共享属性的方式:

 function MyClass() {}; MyClass.prototype.console = {}; // Define a "common" property on the prototype var obj1 = new MyClass(); // Create two instances var obj2 = new MyClass(); Object.getPrototypeOf(obj1).console.id = 13; // Assign a value once... console.log(obj1.console.id); // ... and it exists on both instances console.log(obj2.console.id); 

The shared property is on the prototype object. 共享属性位于原型对象上。

Instead of Object.getPrototypeOf(obj1) you can of course use MyClass.prototype , since you know that obj1 was created by MyClass . 当然,可以使用MyClass.prototype代替Object.getPrototypeOf(obj1) ,因为您知道obj1是由MyClass创建的。 They give the same prototype object. 它们给出相同的原型对象。

If your property always has an object as value and you don't need to replace that value, only mutate it by setting properties on that object, then you don't need to explicitly reference the prototype to set a new value. 如果你的财产总是有一个对象的价值和你没有需要更换价值,只有通过该对象上设置属性发生变异 ,那么你并不需要显式引用原型来设定一个新值。

So this works: 所以这工作:

 function MyClass() {}; MyClass.prototype.console = {}; // Define a "common" property on the prototype var obj1 = new MyClass(); // Create two instances var obj2 = new MyClass(); obj1.console.id = 13; // Assign a value once... (getting console from the prototype) console.log(obj1.console.id); // ... and it exists on both instances console.log(obj2.console.id); 

But if you change console itself, you'll be setting it on the instance, not on the prototype: 但是,如果您更改console本身,则会在实例上而不是在原型上进行设置:

 function MyClass() {}; MyClass.prototype.console = {}; // Define a "common" property on the prototype var obj1 = new MyClass(); // Create two instances var obj2 = new MyClass(); obj1.console = { id: 13}; // Setting an instance property now console.log(obj1.console.id); // ... and it does not exist on both instances console.log(obj2.console.id); // == undefined 

So if that kind of assignment should still work on the prototype, you need to use the first code block with Object.getPrototypeOf or MyClass.prototype . 因此,如果这种分配仍然可以在原型上进行,则需要将第一个代码块与Object.getPrototypeOfMyClass.prototype

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

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