jS OOP 嵌套函数

发布于 2024-12-12 03:07:03 字数 495 浏览 0 评论 0原文

好吧,这可能是一个无趣的问题,因为我对 OOP 很陌生。

我正在尝试构建一些 JS 对象库,并且想知道是否可以使用嵌套函数来完成它?

var object = new function() {

    this.action1 = function () {
        this.dostuff1 = function () {
             return "dostuff1";
        };

     this.dostuff2 = function () {
          return "dostuff2";
     };                   
};

我在访问第三级功能时遇到问题。我可以这样窝吗?

this.action2 = function () {
    return "action2";
}; 

alert(object.action1.dostuff2());

Ok this may be a noobolicious question as im new to OOP.

Im attempting to build something of a JS Object Library and was wondering if I could do it using nested functions??

var object = new function() {

    this.action1 = function () {
        this.dostuff1 = function () {
             return "dostuff1";
        };

     this.dostuff2 = function () {
          return "dostuff2";
     };                   
};

I am having trouble accessing the third level functions. Can I nest like this?

this.action2 = function () {
    return "action2";
}; 

alert(object.action1.dostuff2());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生共白头 2024-12-19 03:07:03

虽然 Eberlin 的答案是完全正确的,但我建议您创建一个嵌套对象,该对象又会再次公开函数而不是嵌套函数本身。否则这可能会成为可维护性的噩梦。

基本上你可以创建

var Child = function(){
   //constructor
};

Child.prototype.doStuff2 = function(){
  return "dostuff2";
};

var Root = function(obj){
   //constructor
   this.child = obj;
};

Root.prototype.action1 = function(){
   return "doStuff1";
};

//usage
var myRoot = new Root(new Child());
myRoot.action1();
myRoot.child.action2();

以下是一个实例: http://jsbin.com/ihotup/edit#javascript,直播

While Eberlin's answer is perfectly correct I'd suggest you to create a nested object which in turn again exposes functions rather than nesting functions itself. Otherwise this might become a maintainability nightmare.

Basically you could create

var Child = function(){
   //constructor
};

Child.prototype.doStuff2 = function(){
  return "dostuff2";
};

var Root = function(obj){
   //constructor
   this.child = obj;
};

Root.prototype.action1 = function(){
   return "doStuff1";
};

//usage
var myRoot = new Root(new Child());
myRoot.action1();
myRoot.child.action2();

Here's a live example: http://jsbin.com/ijotup/edit#javascript,live

凶凌 2024-12-19 03:07:03

请参阅下面的一些代码清理:

var o = (new function () {          // changed 'object' to 'o'
  this.action1 = (function () {     // added parentheses, not required.
    this.dostuff1 = (function () {  // does not return anything.
      return "dostuff1";            // and is also not the proper way to organize
    });                             // ** look at the javascript prototype
    return this;                    // now it does
  });                               // missing closing bracket

  this.dostuff2 = (function () {
    return "dostuff2";
  });                   
});

alert(o.action1().dostuff2());      // action1 is a function, not a variable.

希望这会有所帮助。另外,这里有一个有关 javascript 原型的简要教程

See below for some code cleanup:

var o = (new function () {          // changed 'object' to 'o'
  this.action1 = (function () {     // added parentheses, not required.
    this.dostuff1 = (function () {  // does not return anything.
      return "dostuff1";            // and is also not the proper way to organize
    });                             // ** look at the javascript prototype
    return this;                    // now it does
  });                               // missing closing bracket

  this.dostuff2 = (function () {
    return "dostuff2";
  });                   
});

alert(o.action1().dostuff2());      // action1 is a function, not a variable.

Hope this helps. Also, here's a brief tutorial on the javascript prototype.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文