[JavaScript] Uncaught Typeerror:method get Set.prototype.size called on incompatible receiver

Source: Internet
Author: User
Tags hasownproperty

The Size property cannot be overridden while the set is being extended by method

Scenario: Define a Singletonset, inherit from Set,size can only be 1, and cannot add and remove

The first is the Extend function var extend = (function () {//check for the existence of a bug for (Var p in {Tostring:null}) {//                If it comes in, that means there is no bug return function Extend (o) {for (var i = 1; i < arguments.length; i++) {                var source = Arguments[i];                    For (Var prop in source) {//var desc = object.getownpropertydescriptor (source, prop);                     Object.defineproperty (o, prop, desc);  O[prop] = Source[prop];        The example in the book,}} return o;            }}//If there is a bug, return function Patched_extend (o) {for (var i = 1; i < arguments.length; i++) {            var source = Arguments[i]; Copy the properties that can be enumerated for (Var prop in source) {//var desc = object.getownpropertydescriptor (source, pro                p);                 Object.defineproperty (o, prop, desc);            O[prop] = Source[prop];         }//Check for special properties and copy   for (var j = 0; J < Protoprops.length; J + +) {prop = Protoprops[j];                    if (Source.hasownproperty (prop)) {//var desc = object.getownpropertydescriptor (source, prop);                    Object.defineproperty (o, prop, desc);                O[prop] = Source[prop];    }}} return o; } var protoprops = ["ToString", "ValueOf", "constructor", "hasOwnProperty", "isprototypeof", "Propertyisenumerab Le "," tolocalstring "];}   ());//Call Function Singletonset (member) {this.member = member; This.size = 2;} Singletonset.prototype = Inherit (set.prototype); extend (Singletonset.prototype, {constructor:singletonset, Add:fun        Ction () {throw "Read-only set"}, Remove:function () {throw "Read-only set"}, get Size () {    return 1;    }, Foreach:function (F,context) {f.call (context,this.member); }, Contains:function (x) {return x===this.member; }}); var newset = new Singletonset (1); Console.log (newset.size); Print

Print out to find Newset's Size property error as follows:

After that, you print the test in the Extend function at that location:

  for (var prop in source) {                // var desc = Object.getOwnPropertyDescriptor(source, prop);                // Object.defineProperty(o, prop, desc);                 o[prop] = source[prop];                 console.log(prop);                 console.log(o[prop]);            }

Print as follows:

That is, when executing o[prop] = source[prop]; , the prop===‘size‘ custom method is not assigned to the target object when the error is thrown.

So, my solution, which is commented out, is to make a copy of the Object.defineProperty method to avoid using it o[‘size‘] and throw an error.
The modified extend function is as follows

var extend = (function () {//check for the existence of a bug for (Var p in {Tostring:null}) {//If you come in, that means no bu G return function Extend (o) {for (var i = 1; i < arguments.length; i++) {var source                = Arguments[i];                     For (Var prop in source) {var desc = object.getownpropertydescriptor (source, prop);                    Object.defineproperty (o, prop, desc);  O[prop] = Source[prop];        The example in the book,}} return o;            }}//If there is a bug, return function Patched_extend (o) {for (var i = 1; i < arguments.length; i++) {            var source = Arguments[i];                  Copy the attributes that can be enumerated for (Var prop in source) {var desc = object.getownpropertydescriptor (source, prop);                 Object.defineproperty (o, prop, desc);            O[prop] = Source[prop]; }//Check special properties and copy for (var j = 0; J < Protoprops.length;                J + +) {prop = Protoprops[j];                    if (Source.hasownproperty (prop)) {var desc = object.getownpropertydescriptor (source, prop);                    Object.defineproperty (o, prop, desc);                O[prop] = Source[prop];    }}} return o; } var protoprops = ["ToString", "ValueOf", "constructor", "hasOwnProperty", "isprototypeof", "Propertyisenumerab Le "," tolocalstring "];} ());

At this time

var newSet = new SingletonSet(1);console.log(newSet.size); //打印//结果 为 1,符合预期

[JavaScript] Uncaught Typeerror:method get Set.prototype.size called on incompatible receiver

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.