admin管理员组

文章数量:1426929

According to this document from MDN, Object.prototype.__defineGetter__() should not be used:

Non-standard This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large inpatibilities between implementations and the behavior may change in the future.

Deprecated This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.

On the other hand, this notice mentions just the product sites facing the Web, inpatibilities between implementations, and browser support.

It clearly applies to the client-side. So, I wonder if it is also deprecated for server-side use, and what is the best alternative option.

According to this document from MDN, Object.prototype.__defineGetter__() should not be used:

Non-standard This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large inpatibilities between implementations and the behavior may change in the future.

Deprecated This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.

On the other hand, this notice mentions just the product sites facing the Web, inpatibilities between implementations, and browser support.

It clearly applies to the client-side. So, I wonder if it is also deprecated for server-side use, and what is the best alternative option.

Share Improve this question asked Oct 1, 2015 at 11:15 Luis SieiraLuis Sieira 31.7k4 gold badges32 silver badges55 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

__defineGetter__ and such, which were never standard, were made obsolete by ECMAScript5 (2009) by Object.defineProperty, getter/setter literal syntax in object initializers, and in ECMAScript 2015 ("ES6") by get/set declarations in classes. That syntax is deprecated even in the engines that supported it, regardless of whether the engine is being used in the browser, on a server, or in a non-web application.

Here are examples of the standard syntax. I've included setters in them as well, but of course you'd leave those off for read-only properties.

  1. Object.defineProperty (ES5+, 2009):

    Object.defineProperty(obj, "name", {
        get: function() {
            return "the value";
        },
        set: function(value) {
            // Do something with value
        }
    });
    
  2. Getter/setter literal syntax in object initializers (ES5+, 2009):

    var obj = {
        get name() {
            return "the value";
        },
        set name(value) {
            // Do something with value
        }
    };
    

    (I used var in that example to stick to ES5 features, but all major engines have long supported let and const.)

  3. Getter/setter syntax in classes (ES2015, aka "ES6"):

    class Example {
        get name() {
            return "the value";
        }
        set name(value) {
            // Do something with value
        }
    }
    

Those are all long-supported by all major JavaScript engines (including those used by the vast majority of server-side environments [V8 in Node.js and Deno; JavaScriptCore in Bun]).

New code should use these rather than the old never-standard syntax.

Yes, Object.prototype.__defineGetter__ and its related methods are deprecated everywhere including Node.js not just in browsers.

From MDN:

This feature is deprecated in favor of defining getters using the object initializer syntax or the Object.defineProperty() API. This method's behavior is only specified for web patibility, and is not required to be implemented in any platform. It may not work everywhere.

Despite being deprecated, Object.prototype.__defineGetter__ still works in all modern browsers, and according to MDN is unlikely to be removed:

__defineGetter__() is defined in the spec as "normative optional", which means no implementation is required to implement this. However, all major browsers implement it, and due to its continued usage, it's unlikely to be removed. If a browser implements __defineGetter__(), it also needs to implement the __lookupGetter__(), __lookupSetter__(), and __defineSetter__() methods.

In the spec Object.prototype.__defineGetter__ is described as Normative Optional, Legacy which means pretty much what MDN wrote above. From the Spec:

A conforming implementation of ECMAScript must implement Legacy subclauses, unless they are also marked as Normative Optional. All of the language features and behaviours specified within Legacy subclauses have one or more undesirable characteristics. However, their continued usage in existing applications prevents their removal from this specification. These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code.

As of Chrome 5, Firefox 4, and Safari 5.1 (circa 2011) there is Object.defineProperty which should be used instead. So the only reason to use Object.prototype.__defineGetter__ in new code would be if for some reason you had to support browsers older than that, and then you should only do that in old browsers where Object.defineProperty was not supported.

本文标签: javascriptis defineGetter deprecated in nodejsStack Overflow