0

I started using Base.js for a class inheritance. In Base.js a class should be an object, but does not function.

This creates a problem:

    ​var obj = {
    variable:true,
        func:function(){
            console.log(variable);
        }
    };

    obj.func();​

This code throw error: "Uncaught ReferenceError: variable is not defined". This is because "console.log(variable);" does not have "this".

But i dont want write "this" in ALL functions in my large class.

Is there any way around this?

4

1 回答 1

0

你总是可以多写一行代码: var ins_obj = this; 但是您仍然必须将 ins_obj.variable 放在 console.log 中。我用过的任何语言都会做这些事情。this.variable 是在对象内部调用变量、函数的方式。

于 2012-04-04T07:18:01.850 回答