Javascript 对象。对象实例化为对象的 属性 值

Javascript Objects. Instantiation of an object as the property value of an object

我在 JavaScript...

中围绕对象实例化和作用域遇到一些问题

查看示例代码:

someOtherObj = {
  aMethod: function() {
    $('body').append('aMethod successfully called!<br>');
    return 'd';
  }
}

// THIS WORKS!!
$('body').append(someOtherObj.aMethod() + '<br>');

someObj = {
  aValue: 'a',
  bValue: 'b',
  cValue: this.aValue,              // THIS DOESN'T WORK!!
  dValue: new someOtherObj(),       // THIS DOESN'T WORK!!
  eValue: {
    fValue: aValue,                 // THIS DOESN'T WORK!!
    gValue: this.aValue,            // THIS DOESN'T WORK!!
    hValue: someObj.aValue,         // THIS DOESN'T WORK!!
  },
};

// JavaScript crashes out before this, but...
// This should result in:  'a'
$('body').append(someObj.cValue + '<br>');

// This should result in:  'd'
$('body').append(someObj.dValue.aMethod() + '<br>');

// These should result in: 'a'
$('body').append(someObj.eValue.fValue + '<br>');
$('body').append(someObj.eValue.gValue + '<br>');
$('body').append(someObj.eValue.hValue + '<br>');

我认为这些评论是不言自明的……但话虽这么说:

  1. 如何让cValuereference(===)aValue,and/or有相同的值(==) 作为 aValue?
  2. 如何实例化一个新对象作为另一个对象的属性?
  3. 如何访问包含对象的 属性?

How can I make cValue reference (===) aValue, and/or have the same value (==) as aValue?

你不能(除了使用相同的值来初始化它,例如 cValue: 'a');更多信息:Self references in object literal declarations.

或者,您可以将 cValue 设为 getter 即 returns aValue,尽管在这种情况下没有什么意义:

var obj = {
    aValue: 42,
    get cValue() {
        return this.aValue;
    }
};
console.log(obj.cValue); // 42
obj.aValue = 67;
console.log(obj.cValue); // 67

How can I instantiate a new object as the property of another object?

你做事的方式。您正在做的事情 (new someOtherObj()) 的问题在于 someOtherObj 是一个 对象 ,而不是 构造函数 . (构造函数是对象,但大多数对象不是构造函数。)

这是一个构造函数:

function Example() {
    this.someProperty = "some value"; // This bit is optional; it's here to
                                      // emphasize that constructors initialize
                                      // the contents of the new object
}

也是这样,通过 ES2015 class 语法:

class Example {
    constructor() {
        this.someProperty = "some value"; // Again, this bit is optional
    }
}

在这两种情况下,这都有效:

var obj {
    example: new Example()
};
console.log(example.someProperty); // "some value"

How can I access the property of a containing object?

通过 this,如果 运行 的代码以将 this 设置为对象的方式调用。例如:

var obj = {
    answer: 42,
    doSomething: function() {
        console.log(this.answer);
    }
};
obj.doSomething(); // 42

查看这些问题及其答案:

  • How does the "this" keyword work?
  • How to access the correct this inside a callback? *(关于常见陷阱的建议)

或者在像这样的一次性对象的情况下,通过使用分配给它们的变量的名称:

var obj = {
    answer: 42,
    doSomething: function() {
        console.log(obj.answer); // <== Only change is here
    }
};
obj.doSomething(); // 42

据我了解,this 取决于执行上下文。此外,它不能用于赋值。也就是说:

var obj0 = {
    self: this // <--refers to the global object
}

var obj1 = {
    'use strict'
    self: this // undefined
}

参见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this