久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

有沒有辦法制作“Object.frozen"?嘗試更改對象時

Is there a way to make an quot;Object.frozenquot; object throw warnings when an attempt is made to change it?(有沒有辦法制作“Object.frozen?嘗試更改對象時拋出警告?)
本文介紹了有沒有辦法制作“Object.frozen"?嘗試更改對象時拋出警告?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我使用 Object.freeze 來防止自己違反自己的規則.當我嘗試做一個糟糕的任務時,我希望 Object.freeze 能和我說話.然而, Object.freeze 只是讓分配默默地失??!例如,如果我這樣做

I use Object.freeze as a means to prevent myself from breaking my own rules. I would like Object.freeze to speak to me when I try to make a bad assignment. However, Object.freeze simply makes the assignments silently fail! For example, if I do

/*
 * Frozen singleton object "foo".
 */
var foo = (function() {
  var me = {};

  me.bar = 1;

  if (Object.freeze) {
    Object.freeze(me);
  }

  return me;
})();

foo.bar = 2;
console.log(foo.bar);

控制臺會記錄1",但我不知道我曾經做過錯誤的分配.這當然會導致我的代碼中出現危險的意外行為,而凍結對象的全部目的是為了避免意外.事實上,我更有可能通過不凍結對象、讓錯誤的分配發生以及讓我的代碼稍后因為錯誤的值而失敗來獲得詳細的錯誤輸出.

the console will log "1", but I won't know that I ever made a bad assignment. This of course can lead to dangerous unexpected behavior in my code, when the whole point of freezing the object was to avoid the unexpected. In fact, I'm more likely to get verbose error output by not freezing the object, letting the bad assignment take place, and having my code fail later on because of the bad value.

我想知道 JavaScript 在任何瀏覽器中是否有任何隱藏的不可變對象警告"編譯指示,以便我可以知道何時嘗試對Object.frozen"對象進行變異.

I'm wondering if JavaScript has any hidden "immutable object warning" pragma in any browser, so that I can know when I attempt to mutate an "Object.frozen" object.

推薦答案

嚴格模式下的代碼在嘗試分配給不可寫屬性時會拋出 TypeError (ECMA-262: 11.13.1).但請注意,您不能依賴在不完全支持 ES5 嚴格模式(例如 IE9)的瀏覽器中引發的錯誤.

Code in strict mode will throw a TypeError when trying to assign to an unwritable property (ECMA-262: 11.13.1). But do notice you cannot rely on the error being thrown in browsers that don't fully support ES5 strict mode (such as IE9).

要讓你的代碼在嚴格模式下運行,請在包含代碼的 JS 文件或函數開頭添加 'use strict'; 并在實現嚴格模式的環境中運行(參見例如此列表:http://caniuse.com/#feat=use-strict).

To make your code run in strict mode, add 'use strict'; at the beginning of the JS file or function containing the code and run it in an environment that implements strict mode (see for example this list: http://caniuse.com/#feat=use-strict).

這篇關于有沒有辦法制作“Object.frozen"?嘗試更改對象時拋出警告?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 亚洲国产成人精品久久久国产成人一区 | 亚洲欧美国产毛片在线 | 免费黄色在线观看 | 9久久| 午夜视频一区 | 日本精品裸体写真集在线观看 | 国产在线精品一区 | 中文字幕乱码视频32 | 国产一区二区av | 蜜桃毛片 | 日本精a在线观看 | 亚洲国产精品精华素 | 欧美日韩1区2区 | 国产91一区 | 日本免费在线看 | 香蕉视频在线播放 | 欧美一级片在线播放 | 天天干天天爱天天操 | 毛色毛片免费看 | 国产一区二区 | 免费看黄色视屏 | 一级片在线观看 | 午夜免费福利影院 | 天天操人人干 | 亚洲欧洲国产视频 | 午夜网站视频 | 亚洲国产情侣自拍 | 日韩欧美黄色 | 亚洲成人一区二区三区 | 亚洲精品日韩综合观看成人91 | 日韩精品一区二区三区中文在线 | 亚洲国产精品久久久久婷婷老年 | 亚洲精品一区二区三区中文字幕 | 日韩成人在线网址 | 99在线播放 | 精品不卡 | 国产精品日韩欧美一区二区三区 | 免费成人高清在线视频 | 欧美色性| 亚洲欧美国产毛片在线 | 亚洲一区二区三区久久久 |