简体   繁体   English

当我将 object 分配给 object 的属性时,该属性会变成 object 吗? 如果是这样,我如何通过第二个访问第一个 object?

[英]When I assign an object to a property of an object, does the property become an object? If so, how do I access the first object via the second one?

I have divided this question into several sub-questions, because they are all related, and make up the pieces to what I do not understand.我把这个问题分成了几个子问题,因为它们都是相关的,并且对我不明白的部分进行了补充。

First and foremost, when I assign an object to a property, does that property become an object too?首先,当我将 object 分配给属性时,该属性是否也变为 object?

Example:例子:

 var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 }

Here, I instantiate an object object1 , and then I instantiate another object, newobject , and assign the first object, object1 as a value to the property obname of the newobject object. Here, I instantiate an object object1 , and then I instantiate another object, newobject , and assign the first object, object1 as a value to the property obname of the newobject object.

Now, is obname now an object?现在, obname现在是 object 吗? How should I view obname right now?我现在应该如何查看obname

This leads me to the sub-second question.这将我引向了第二个问题。

How do I now access the properties of object1 via newobject ?我现在如何通过newobject访问object1的属性?

 var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname);

  • Targeting the obname returns [object object] .obname为目标返回[object object] Why?为什么?
  • Targeting obname.object1 returns undefined .定位obname.object1返回undefined Why?为什么?
  • Targeting obname.object1.color returns error.定位obname.object1.color返回错误。 Why?为什么?

I can only access the properties of object1 this way:我只能通过这种方式访问object1的属性:

 var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname.color);

..by only targeting obname , and then the property of the object1 . ..仅针对obname ,然后是object1的属性。

The last important sub-question: Why does this work?最后一个重要的子问题:为什么会这样?

color is not a property of obname , so why am I able to access it this way? color不是obname的属性,那么为什么我可以通过这种方式访问它?

You've asked several questions here, so I'll try to hit them all....你在这里问了几个问题,所以我会试着把它们都打一遍......

 var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 }

Now, is obname now an object?现在, obname现在是 object 吗? How should I view obname right now?我现在应该如何查看obname

Technically, obname is a "property" or "key" name of the newObject object and it stores a reference to the object1 object.从技术上讲, obnamenewObject object 的“属性”或“键”名称,它存储对object1 object 的引用。

Next:下一个:

 var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname.color);

How do I now access the properties of object1 via newobject?我现在如何通过 newobject 访问 object1 的属性?

The same way to do of any other object, but you will have to access the property of the first object that stores a reference to the second one, so you'll use two levels of "dot notation"对任何其他 object 执行相同的方法,但您必须访问第一个 object 的属性,该属性存储对第二个的引用,因此您将使用两个级别的“点表示法”

 var object1 = new Object(); object1.color = "red"; var newobject = { "obname": object1 } alert(newobject.obname.color);

Targeting the obname returns [object object] .obname为目标返回[object object] Why?为什么?

Because you've attempted to "print" (via an alert ) an entire object and that's what you get when you try to treat an entire object as a string.因为您尝试“打印”(通过alert )整个 object ,这就是您尝试将整个 object 视为字符串时得到的结果。

Targeting obname.object1 returns undefined.以 obname.object1 为目标返回未定义。 Why?为什么?

Because object1 is not declared within obname , it's "referenced" via the obname property.因为object1没有在obname中声明,所以它是通过obname属性“引用”的。 Think of this as a pointer... obname points to where object1 can be found.将此视为一个指针... obname指向可以找到object1的位置。

Targeting obname.object1.color returns error.定位 obname.object1.color 会返回错误。 Why?为什么?

Because obname is a pointer to object1 and object1 doesn't have a property called object1 .因为obname是指向object1的指针,而object1没有名为object1的属性。

You can simply think of obname is equivalent to object1 (It is an Object).你可以简单的认为obname等价于object1(It is an Object)。

As you do access object1 properties (In this case property is color) You should be getting the value of the property as following:当您访问 object1 属性时(在这种情况下,属性是颜色)您应该获得如下属性的值:

object1.color which returns the value "red" object1.color 返回值“red”

  • Targeting the obname returns [object object].以 obname 为目标返回 [object object]。 Why?为什么? Because obname is an Object.因为 obname 是 Object。

Think of it as:把它想象成:

var newobject = { 
  "obname": { "color" : "red"}
}
  • Targeting obname.object1 returns undefined.以 obname.object1 为目标返回未定义。 Why?为什么?
  • Targeting obname.object1.color returns error.定位 obname.object1.color 会返回错误。 Why?为什么?

Because obname does not have any property called object1.因为 obname 没有任何名为 object1 的属性。 Instead obname and object1 are equivalent Objects (they have one property color as defined by yourself).相反, obname 和 object1 是等效的对象(它们具有您自己定义的一种属性颜色)。

alert(newobject.obname.color);

This works because you are accessing obname property correctly.这是有效的,因为您正在正确访问 obname 属性。

To answer the question why is color a property of obname;回答为什么颜色是 obname 的属性的问题; That is because you have assign obname to object1 so they are the same那是因为您已将 obname 分配给 object1 所以它们是相同的

如何修复“无法分配给 object 的只读属性“导出”'#<object> ' 将第二个 function 添加到 module.exports 时?<div id="text_translate"><p> 不幸的是,我已经被困了大约 5 天了。 我用谷歌搜索过,没有什么对我有用。 我有一个 utils.js 文件,里面有很多东西可以帮助我。 当我将 function "doesUserContainRoles" 添加到导出时,我的 Chrome 控制台给了我错误:</p><blockquote><p> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'</p></blockquote><p> 当我删除 function 时,utils 文件中的所有内容都可以完美运行,根本没有 0 个问题。 只要我添加 function,我就会收到此错误。 我通过以下方式要求 utils 文件:</p><pre class="lang-js prettyprint-override"> const utils = require("../../../utils");</pre><p> 并且没有与此一起使用的import 。 (我知道module.exports和import不能一起工作)</p><p> 我正在使用什么:</p><ul><li><p> Vue.js @vue/cli 4.5.8</p></li><li><p> Node.js v15.2.0</p></li></ul><h3> 文件:</h3><p> utils.js</p><pre class="lang-js prettyprint-override"> const winston = require("winston"); const { datadog } = require("./credentials.js"); const { createLogger, format, transports } = require('winston'); const base_url = "http://localhost:3001/api/v1" // Winston ~ const httpTransportOptions = { host: datadog.logger.host, path: datadog.logger.path, ssl: datadog.logger.ssl }; const customLevels = { levels: { emergency: 0, alert: 1, critical: 2, error: 3, warn: 4, notice: 5, info: 6, debug: 7, success: 8 }, } const logger = createLogger({ level: 'info', levels: customLevels.levels, exitOnError: false, format: format.json(), transports: [ new transports.Http(httpTransportOptions), ], }); const commonMessages = { accessPage: "User Accessed a Page", restrictedPage: "Attempt at accessing restricted page" } function alertGeneral() { alert("Oops. An error has occurred. Please try again later, If this problem continues. please contact Vinniehat;"), } function doesUserContainRoles(userRoles. containsRoles) { return.;userRoles.some(role =&gt; containsRoles,includes(role)), } module,exports = { logger, commonMessages, base_url, alertGeneral, doesUserContainRoles }</pre><p> 这是错误堆栈:</p><pre> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;' at Module.eval (utils.js?1b23:45) at eval (utils.js:68) at Module../utils.js (app.js:1944) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (router.js?9883:19) at Module../src/router/router.js (app.js:1812) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (main.js:34)</pre><p> 我也尝试使用export default 。 这样做时,我的前端工作。 网站加载没有错误。 不过,我的后端使用 Express.js 运行,然后引发错误:</p><pre> Unhandled rejection E:\Development\Websites\iceberg-gaming-website\utils.js:45 export default { ^^^^^^ SyntaxError: Unexpected token 'export' at wrapSafe (node:internal/modules/cjs/loader:1018:16) at Module._compile (node:internal/modules/cjs/loader:1066:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at Object.&lt;anonymous&gt; (E:\Development\Websites\iceberg-gaming-website\express\modules\user.js:9:15) at Module._compile (node:internal/modules/cjs/loader:1102:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at E:\Development\Websites\iceberg-gaming-website\express\express.js:27:27 at tryCatcher (E:\Development\Websites\iceberg-gaming-website\node_modules\bluebird\js\release\util.js:16:23)</pre></div></object> - How do I fix 'Cannot assign to read only property 'exports' of object '#<Object>' when adding a 2nd function to module.exports?

暂无
暂无

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

相关问题 如何通过另一个包含一个对象的对象访问一个对象,该对象是我要访问的对象的属性? - How do I access an object via another object that contains an object which is a property of the object I want to have access to? 如何访问字符串对象的属性? - How do I access the property of an object that is a string? 如何将函数分配给 Javascript 对象的属性? - How do I assign a function to the property of a Javascript object? 如何在对象的属性上添加属性? - How do I add a property on a property of an object? 如何访问在运行时添加到对象的属性? - How do I access a property that I added during runtime to an object? 如何通过参数添加对象属性? - How do I add an object property via an argument? 如何通过引用变量更改对象属性? - How do I change an object property via a reference variable? 如何修复“无法分配给 object 的只读属性“导出”'#<object> ' 将第二个 function 添加到 module.exports 时?<div id="text_translate"><p> 不幸的是,我已经被困了大约 5 天了。 我用谷歌搜索过,没有什么对我有用。 我有一个 utils.js 文件,里面有很多东西可以帮助我。 当我将 function "doesUserContainRoles" 添加到导出时,我的 Chrome 控制台给了我错误:</p><blockquote><p> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'</p></blockquote><p> 当我删除 function 时,utils 文件中的所有内容都可以完美运行,根本没有 0 个问题。 只要我添加 function,我就会收到此错误。 我通过以下方式要求 utils 文件:</p><pre class="lang-js prettyprint-override"> const utils = require("../../../utils");</pre><p> 并且没有与此一起使用的import 。 (我知道module.exports和import不能一起工作)</p><p> 我正在使用什么:</p><ul><li><p> Vue.js @vue/cli 4.5.8</p></li><li><p> Node.js v15.2.0</p></li></ul><h3> 文件:</h3><p> utils.js</p><pre class="lang-js prettyprint-override"> const winston = require("winston"); const { datadog } = require("./credentials.js"); const { createLogger, format, transports } = require('winston'); const base_url = "http://localhost:3001/api/v1" // Winston ~ const httpTransportOptions = { host: datadog.logger.host, path: datadog.logger.path, ssl: datadog.logger.ssl }; const customLevels = { levels: { emergency: 0, alert: 1, critical: 2, error: 3, warn: 4, notice: 5, info: 6, debug: 7, success: 8 }, } const logger = createLogger({ level: 'info', levels: customLevels.levels, exitOnError: false, format: format.json(), transports: [ new transports.Http(httpTransportOptions), ], }); const commonMessages = { accessPage: "User Accessed a Page", restrictedPage: "Attempt at accessing restricted page" } function alertGeneral() { alert("Oops. An error has occurred. Please try again later, If this problem continues. please contact Vinniehat;"), } function doesUserContainRoles(userRoles. containsRoles) { return.;userRoles.some(role =&gt; containsRoles,includes(role)), } module,exports = { logger, commonMessages, base_url, alertGeneral, doesUserContainRoles }</pre><p> 这是错误堆栈:</p><pre> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;' at Module.eval (utils.js?1b23:45) at eval (utils.js:68) at Module../utils.js (app.js:1944) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (router.js?9883:19) at Module../src/router/router.js (app.js:1812) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (main.js:34)</pre><p> 我也尝试使用export default 。 这样做时,我的前端工作。 网站加载没有错误。 不过,我的后端使用 Express.js 运行,然后引发错误:</p><pre> Unhandled rejection E:\Development\Websites\iceberg-gaming-website\utils.js:45 export default { ^^^^^^ SyntaxError: Unexpected token 'export' at wrapSafe (node:internal/modules/cjs/loader:1018:16) at Module._compile (node:internal/modules/cjs/loader:1066:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at Object.&lt;anonymous&gt; (E:\Development\Websites\iceberg-gaming-website\express\modules\user.js:9:15) at Module._compile (node:internal/modules/cjs/loader:1102:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at E:\Development\Websites\iceberg-gaming-website\express\express.js:27:27 at tryCatcher (E:\Development\Websites\iceberg-gaming-website\node_modules\bluebird\js\release\util.js:16:23)</pre></div></object> - How do I fix 'Cannot assign to read only property 'exports' of object '#<Object>' when adding a 2nd function to module.exports? 如何访问 JavaScript 中 object 的属性? - How can I access a property of an object in JavaScript? 如何访问每个 JSON 对象的属性? - How do I access a property of each JSON object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM