簡體   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?

我把這個問題分成了幾個子問題,因為它們都是相關的,並且對我不明白的部分進行了補充。

首先,當我將 object 分配給屬性時,該屬性是否也變為 object?

例子:

 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.

現在, obname現在是 object 嗎? 我現在應該如何查看obname

這將我引向了第二個問題。

我現在如何通過newobject訪問object1的屬性?

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

  • obname為目標返回[object object] 為什么?
  • 定位obname.object1返回undefined 為什么?
  • 定位obname.object1.color返回錯誤。 為什么?

我只能通過這種方式訪問object1的屬性:

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

..僅針對obname ,然后是object1的屬性。

最后一個重要的子問題:為什么會這樣?

color不是obname的屬性,那么為什么我可以通過這種方式訪問它?

你在這里問了幾個問題,所以我會試着把它們都打一遍......

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

現在, obname現在是 object 嗎? 我現在應該如何查看obname

從技術上講, obnamenewObject object 的“屬性”或“鍵”名稱,它存儲對object1 object 的引用。

下一個:

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

我現在如何通過 newobject 訪問 object1 的屬性?

對任何其他 object 執行相同的方法,但您必須訪問第一個 object 的屬性,該屬性存儲對第二個的引用,因此您將使用兩個級別的“點表示法”

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

obname為目標返回[object object] 為什么?

因為您嘗試“打印”(通過alert )整個 object ,這就是您嘗試將整個 object 視為字符串時得到的結果。

以 obname.object1 為目標返回未定義。 為什么?

因為object1沒有在obname中聲明,所以它是通過obname屬性“引用”的。 將此視為一個指針... obname指向可以找到object1的位置。

定位 obname.object1.color 會返回錯誤。 為什么?

因為obname是指向object1的指針,而object1沒有名為object1的屬性。

你可以簡單的認為obname等價於object1(It is an Object)。

當您訪問 object1 屬性時(在這種情況下,屬性是顏色)您應該獲得如下屬性的值:

object1.color 返回值“red”

  • 以 obname 為目標返回 [object object]。 為什么? 因為 obname 是 Object。

把它想象成:

var newobject = { 
  "obname": { "color" : "red"}
}
  • 以 obname.object1 為目標返回未定義。 為什么?
  • 定位 obname.object1.color 會返回錯誤。 為什么?

因為 obname 沒有任何名為 object1 的屬性。 相反, obname 和 object1 是等效的對象(它們具有您自己定義的一種屬性顏色)。

alert(newobject.obname.color);

這是有效的,因為您正在正確訪問 obname 屬性。

回答為什么顏色是 obname 的屬性的問題; 那是因為您已將 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.

相關問題 如何通過另一個包含一個對象的對象訪問一個對象,該對象是我要訪問的對象的屬性? 如何訪問字符串對象的屬性? 如何將函數分配給 Javascript 對象的屬性? 如何在對象的屬性上添加屬性? 如何訪問在運行時添加到對象的屬性? 如何通過參數添加對象屬性? 如何通過引用變量更改對象屬性? 如何修復“無法分配給 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> 如何訪問 JavaScript 中 object 的屬性? 如何訪問每個 JSON 對象的屬性?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM