constructor.name is undefined in Internet Explorer

前端 未结 6 1277
孤独总比滥情好
孤独总比滥情好 2021-02-13 05:11

My debugging work in IE ended today by finding that constructor.name is undefined.

I created the following simple code that reproduces the issu

6条回答
  •  生来不讨喜
    2021-02-13 05:24

    Perhaps this example clears up some confusion.

    var number = 10;        //A global variable. Part of the global window object.
    //window.number = 10;   //Does exactly the same as the line above.
    
    
    var customer = {
        number: 20,
        speak: function () {
            if (this === window) {
                alert('I am being called by go() ' + this.number);      //this.number points to the global variable number
            } else {
                alert('I am being called by customer.speak ' + this.number);  //this.number points to the customer member number
            }
        }
    }
    
    var go = customer.speak;  //Go now points directly to the function called "speak" as if it is not a part of the customer object. The this parameter of speak will be the global window object;
    
    go();
    customer.speak();         //Now the speak function is called a part of the customer object. The this parameter of speak will be the customer object
    

提交回复
热议问题