javascript中的子对象函数 [英] sub object functions in javascript

查看:85
本文介绍了javascript中的子对象函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以使用子对象和函数创建文字对象:

I know you can create literal objects with subobjects and functions:

var obj = {
    val : 1,
    level1 : {
        val : 2,
        val2 : 3,
        func : function(){
            return this.val2
        }
    }
}

console.log(obj.val);
console.log(obj.level1.val);
console.log(obj.level1.func());

输出:

1
2
3

我想要什么do是对象中的方法做同样的事情,类似于:

What I would like to do is do the same thing with methods in a object, something similar to:

function objType() {
    this.val = 1;
    this.func = function(){
        return this.val;
    }
    this.level1 = function(){
        this.val = 2;
        this.func = function(){
            return this.val;
        }
        this.level2 = function(){
            this.val = 3;
            this.func = function(){
                return this.val;
            }
        }
    };
};

然后我预期:

var obj = new objType();
console.log(obj.func());
console.log(obj.level1.func());
console.log(obj.level1.level.func());

输出:

1
2
3

但是,只有在脚本抛出错误之前,第一个console.log输出。

However, only the first console.log outputs before the script throws an error.

有没有办法在Javascript中使用子方法?

Is there any way to have sub methods in Javascript?

- 编辑 -

我的目标是创建一个类,我可以用来在屏幕中间显示一个框,用于显示消息,问题(得到是/否回答)和表格。我正在考虑使用子方法构建它的好方法,以便随后可以引用它:

My goal is to create a class i can use for showing a box in the middle of the screen, for displaying messages, questions(to get a yes/no response), and forms. I was thinking a good way to structure it would be with sub methods, so that it could then be referenced with:

function box() {
    this.show = function(){
        //multiple sub methods here
    }
    this.hide = function(){
        //code to hide window here
    }
}

var aBox = new box();
aBox.show.message('the message');
aBox.hide();
aBox.show.question('the question');
aBox.hide();

- 编辑 -
感谢@Sean Vieira

--edit-- thanks @Sean Vieira

为了完整性,我将使用他的解决方案将我的代码的修改版本放在这里:

For completeness I'll put the modified version of my code here using his solution:

function objType() {
    this.val = 1;
    this.func = function(){
        return this.val;
    }
    this.level1 = {
        val : 2,
        func : function(){
            return this.val;
        },
        level2 : {
            val : 3,
            func : function(){
                return this.val;
            }
        }
    }

var obj = new objType();
console.log(obj.func());
console.log(obj.level1.func());
console.log(obj.level1.level.func());

输出

1
2
3


推荐答案

你可以通过链接轻松完成这项工作

you can do this easily with chaining

function Box() {
    this.show = function(){
       //your code goes here
       return this;

    },
    this.message = function(message){
       //code goes here
       return this;
    }
  }

var aBox = new Box();
aBox.message('the message').show()

这篇关于javascript中的子对象函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆