javascript - 了解 JavaScript 面向对象

标签 javascript oop

好的,这真的是我第一次用 javaScript 上第一个类,几个月来我一直在学习如何使用 JQuery,所以我真的不难教。在这段代码中,我会问很多问题..

好的,这是我的第一个 javascript 类的代码

window.myclass = function(){
    this.n1 = 0;
    this.n2 = 0;
    this.ans = 0;
    this.hehe = 0;

    this.defaultNumbersAdd = function(){

       this.hehe =setInterval(function(){
          //just an example of inert function...
          //the truth is i can't grab the this.n1 and this.n2...
          this.ans = this.n1 + this.n2;
       },100);
       clearTimeout(this.hehe);
    };

    this.returnAns = function(){
       return this.ans;
    };

    this.getAnswer = function(){
        this.defaultNumbersAdd(); //i want to reuse this function but I can't access it
        return this.returnAns(); //and also this one
    };

    this.modifynumbers = function(n1,n2){
        this.n1 = n1;
        this.n2 = n2;
    };
};

var myclassins = new window.myclass();
alert(myclassins.getAnswer);

现在我的问题是

1) 我如何获取要在我的函数中使用的类变量 (this.vars)

2) 如何在另一个函数中使用其他函数

注意:抱歉,如果我的解释有点含糊...

最佳答案

直接位于 window.myclass 对象内部的 this 指针指向该对象本身,您使用它来定义该对象的本质属性,例如 n1 和 n2。

但是,在您的对象中定义的函数中,this 指针指的是函数,而不是对象。 ***但是,在setTimeouts 和setIntervals 中,“this”关键字指的是窗口对象,或者全局范围。 (例如,请参阅底部的更新。)

因此,您可以直接访问属性,使用您用于实例化类的全局范围内的变量名称,

    this.returnAns = function(){
       return myclassins.ans;
    };

但是,上述方法将您的类与实例化时赋予对象的名称联系在一起,并且仅对单个对象有用。

更好的解决方案是在对象中定义一个变量并使用它引用“this”指针:

window.myclass = function(){
    this.n1 = 0;
    this.n2 = 0;
    this.ans = 0;
    this.hehe = 0;
    var _self = this;   // reference to "this"

    this.defaultNumbersAdd = function(){

       myclass.hehe =setInterval(function(){
          //just an example of inert function...
              //the truth is i can't grab the this.n1 and this.n2...
          _self.ans = _self.n1 + _self.n2;
          console.log(_self.ans);
       },100);
       clearTimeout(_self.hehe);
    };

    this.returnAns = function(){
       return _self.ans;
    };

    this.getAnswer = function(){
        this.defaultNumbersAdd(); //i want to reuse this function but I can't access it
        return _self.returnAns(); //and also this one
    };

    this.modifynumbers = function(n1,n2){
        _self.n1 = n1;
        _self.n2 = n2;
    };
};

最后,另一种技术是使用闭包定义函数,将“this”指针传递给返回另一个函数的函数,如下所示:

    this.modifynumbers = (function(__this) {
        return function(n1,n2){
            __this.n1 = n1;
            __this.n2 = n2;
        }
    })(this);

当然,最简单的方法是使用var self;然而,在 JavaScript 中有几种不同的方法来完成一项任务,这些技术在不同的场景中都可以派上用场。

更新:

@apsillers 指出,当在 setTimeout 和 setInterval 函数调用的回调中引用时,“this”关键字指向窗口对象。这是一个例子:

// top level scope
var global = "globalvalue";

window.demo = function() {
    this.somevar = "somevalue";        

    this.someFunct = function() {
        console.info("somevar = " + this.somevar);  // prints "somevalue"
        console.info("global = " + this.global);   // prints undefined! this points to "demo"!

        setTimeout( function() {
            console.info("timeout - somevar = " + this.somevar);  // undefined! references the "window" context
            console.info("timeout - global = " + this.global);   // prints "globalvalue"
        },1000);
    };
};
var dem = new demo();

dem.somevar;   // accessible from outside the object. prints "somevalue"
dem.someFunct();

someFunct() 的输出:

# inside method of object
somevar = somevalue
global = undefined

# inside timeout
timeout - somevar = undefined
timeout - global = globalvalue

如您所见,在 setTimeout 内部,“this”明确指向窗口或全局范围! (注意:您实际上可以使用那个小代码片段并在您的 Chrome 或 Firebug 调试器中运行它。)

关于javascript - 了解 JavaScript 面向对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712992/

相关文章:

vb.net - OOP - 在哪里调用数据访问层?

java - Vert.x - java.lang.IllegalStateException : No language implementation known for prefix D

javascript - 如何隐藏 HTML 元素不出现在仅用于桌面的其他页面上?

javascript - IE8 : document. 写入打开的窗口给我 "Access is denied"

java - 为什么构造函数在java中不被继承?

c++ - 是否需要删除未实例化为变量的指针?

javascript - 毕竟 Hook 错误 - 使用 cucumber-js 和 selenium 时 'this' 并未引用我的世界构造函数

javascript - React JS 排序问题

c++ - 指向成员函数的函数指针

python - 如何组织Python相关类