This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
When I checked the documentation of MDN, I found several apis I had never heard of or used before. I tried the usage and return value of several apis with curiosity.
Also, note the criteria:
Non-standard: This feature is non-standard, so try not to use it in production!
Deprecated: This feature has been removed from the Web standards, and while some browsers still support it, it may be discontinued at some point in the future, so try not to use it.
Object.prototype.__defineGetter__()
The __defineGetter__ method binds a function to a specified property of the current object. When the value of that property is read, the function you bound to is called.
Parameters:
-
Prop A string representing the name of the specified property.
-
Func A function that is automatically called when the value of a prop property is read.
Case study:
var o = {}; o.__defineGetter__('gimmeFive', function() { return 5; }); console.log(o.gimmeFive); // 5 // Please try to use the following two recommendations instead: // 1. Use get syntax in object literals var o = {get gimmeFive() {return 5; }}; console.log(o.gimmeFive); // 5 // 2. Use object.defineProperty. Object.defineProperty(o, 'gimmeFive', { get: function() { return 5; }}); console.log(o.gimmeFive); / / 5Copy the code
Object.prototype.__defineSetter__()
The __defineSetter__ method binds a function to a specified property of the current object. When that property is assigned, the function you bound to will be called.
-
Prop A string representing the name of the specified property.
-
Fun A function called when trying to assign a value to the sprop property. Normally you give this function an argument: function(val) {..}
-
Val The name of an arbitrary argument whose value is the value that was tried to assign to the sprop property when fun was called.
Usage Description: The __defineSetter__ method can set (create or modify) accessor attributes for an existing object, whereas the set syntax (en-us) in object literals can only be used when creating a new object.