开发者

multi dimensional object

how can you make a multi dimensional object??

function tst(){
    this.a = function(){
        alert(this.b.abc.c());
    };

    this.b = function(){

    };
}
var obj = new tst();
obj.b.abc = function(){
    this.c = function(){
        return 'hello 开发者_JAVA技巧world';
    };
};

obj.a();


The problem is with this code:

obj.b.abc = function(){
    this.c = function(){
        return 'hello world';
    };
};

Calling the abc function will not create a obj.b.abc.c function, but a obj.b.c function. Therefore, this.b.abc.c() throws an error because such a function doesn't exist.


This will make it work:

function tst() {
    this.a = function() {
        alert( this.b.abc.c() );
    };

    this.b = function() {

    };
}

var obj = new tst();

obj.b.abc = function() {
    this.abc.c = function() { // <--- NEW 
        return 'hello world';
    };
};

obj.b.abc(); // <--- NEW
obj.a();

Live demo: http://jsfiddle.net/vgpNU/


Seems to be working fine here:

http://jsfiddle.net/maniator/e3pfu/

function tst(){
    this.a = function(){

    };

    this.b = function(){

    };
}
var obj = new tst();
obj.b.abc = function(){
    alert(this);

    this.c = function(){

    };
};

obj.b.abc();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜