猿问

在function函数内使用this绑定公开属性的疑问?

var Shape = function(width, height) {

    var self = this;

    self.area = function() {

        return width * height;

    }

    return self;

};


var shape = Shape(20, 30);

area;

为什么这里area变成了全局的?

弑天下
浏览 433回答 1
1回答

智慧大石

Shape(20,30)===>window.Shape(20,30)this此时为window对应的window.area = function(){}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答