[英]Javascript: Should an argument passed to a function be defined as 'let', 'const' or 'var'?
[英]In javascript, how to defined a CONST function?
我在chrome擴展程序上遇到一些問題,無法按需編輯頁面中的一個功能以采取行動。 我想在文檔加載開始時創建一個函數名。 然后,頁面中的現有腳本無法使用該名稱創建一個新函數,因此必須在加載文檔時使用我的第一個函數。
所以我想設置一個CONST函數,該函數不能由頁面的所有其他腳本再次設置。
有沒有辦法做到這一點。
例:
function unchangeable(){
alert()
}
//some code expanding ........, and new function name unchangeable() can not be set again
function unchangeable(){ ....
..........
}
//this new function will fire an error, and when we call unchangeable() from now, he still create an alert()
或者在他第一次被調用之前,有什么方法可以編輯一個函數?
您可能可以為此使用Object.defineProperty
。 您應該能夠將writable
(和configurable
,我認為)設置為false,以使該函數按您的意願運行。
Object.defineProperty(window, 'unchangeable', {
value: function(){
alert();
},
enumerable: true,
writable: false,
configurable: false
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.