简体   繁体   English

窗口上的Javascript getter / setter

[英]Javascript getters/setters on window

Why does the following throw a TypeError in firefox 21.0? 为什么以下内容在firefox 21.0中引发TypeError?

Object.defineProperty(window,'windowProperty',{
    get: function(){
        return 'windowProperty'
    },

    set: function(val){
        console.log('windowProperty is being set');
    },

    configurable: true,
});

var windowProperty;

But declaring windowProperty without using var works: 但是在不使用var的情况下声明windowProperty是可行的:

windowProperty;

Or 要么

window.windowProperty;

This behavior is also present in spidermonkey: 蜘蛛猴中也存在此行为:

var a=1;

Object.defineProperty(this,'a',{
    get: function(){
        return 'a';
    },
});

Writing only 只写

windowProperty;

does not declare a variable. 不声明变量。 It just tries to return the variable content in the closest context or undefined if it can't be found. 它只是尝试在最接近的上下文中返回变量内容,如果找不到,则undefined It's just like an assignment without a target. 就像没有目标的任务。 For example, you can also write random text without throwing an error: 例如,您还可以编写随机文本而不会引发错误:

'Hello, world !';
123456;
undefined;

Using var instead try to redefine a property which is already defined before in your code, so you have an error. 而是使用var尝试重新定义代码中之前已定义的属性,因此会出现错误。

EDIT 编辑
As precised by simonzack, the browser does not always send an error when redefining a variable. 根据simonzack的精确描述,浏览器在重新定义变量时并不总是发送错误。 For example: 例如:

var test; var test;

doesn't throw this error (even if it is a bad idea and some JS validators will warn you). 不会抛出此错误(即使这是一个坏主意,某些JS验证程序也会警告您)。 By defining getters and setters, however, the browser 'locks' this property (to prevent collision, for example, two different setters on the same property). 但是,通过定义getter和setter,浏览器会“锁定”此属性(例如,为了防止冲突,同一属性上的两个不同的setter)。 My bad, it's a misinterpretation. 不好,这是一种误解。

Is there any reason why you want to redefine your variable? 有什么理由要重新定义变量?

EDIT 2 编辑2
Taking into account that the var declaration went before the defineProperty , it allows to adds precision to my explanations. 考虑到var声明位于defineProperty之前,因此可以为我的解释增加精度。 Indeed, when you first use the var declaration, the browser set its configurable state to false, which prevent doing changes on its descriptor ( See link ). 确实,当您第一次使用var声明时,浏览器将其可configurable状态设置为false,从而防止对其描述符进行更改( 请参阅link )。 So when you try to change it with the defineProperty function, it causes an error. 因此,当您尝试使用defineProperty函数对其进行更改时,它将导致错误。 An example to see better would be to wrap the code in a closure function. 一个更好看的例子是将代码包装在闭包函数中。 This way the windowProperty defined by the var is not the same and everything is ok. 这样,由var定义的windowProperty不相同,一切正常。

(function () {
    var windowProperty;
    Object.defineProperty(...);
    //It works because the previously defined variable is in the scope of the function and thus is not the same as window.windowProperty.
})();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM