小编jth*_*mas的帖子

在Javascript中确保"此"上下文的最佳做法是什么?

这是一个带有公共和私有方法的简单Javascript类的示例(小提琴:http://jsfiddle.net/gY4mh/).

function Example() {
    function privateFunction() {
       // "this" is window when called.
       console.log(this);
    }

    this.publicFunction = function() {
       privateFunction();
    }
}

ex = new Example;
ex.publicFunction();
Run Code Online (Sandbox Code Playgroud)

从公共部门调用私有函数会导致"this"成为窗口对象.我应该如何确保使用类上下文而不是窗口调用我的私有方法?这会不合适吗?

javascript this

9
推荐指数
1
解决办法
1376
查看次数

标签 统计

javascript ×1

this ×1