javascript - 是否可以嵌套 JavaScript 对象?

标签 javascript class oop object nested

这似乎是不可能的。还有另一种巧妙的方法可以实现此目的吗?

// Our transitions class
function Transitions() {
    this.fade = function() {
            this.create = function() {
                alert('you have arrived at the create method of the fade class');
            }
    }
}

最佳答案

这是可能的。您现在使用它的方式。

您可以这样做来调用创建函数:

var x = new Transitions();
// Here .create does not exists
x.fade(); // Here the create function will be generated to the this object
x.create(); // Does alert

但是你可能想要这样的东西:

function Transitions() {
        // Create an object with an sub object
        this.fade = {
                create : function() {

                    alert('you have arrived at the create method of the fade class');
                }
        }

    }

var x = Transitions();
x.fade.create(); // Call the sub object

关于javascript - 是否可以嵌套 JavaScript 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21412709/

相关文章:

javascript - 通过 Javascript DOM 完成时未调用 SVG 动画元素的 endEvent 处理程序

php - 为什么 PHP 5.2+ 不允许抽象静态类方法?

javascript - Sequelize 保存/创建方法仅适用于 CRUD 应用程序的第一次

javascript - 如何在 CodeIgniter 中使用 jquery 复制隐藏字段值并将其传递到另一个页面?

c++ - 断开节点并返回它

c# - 两个程序集中具有相同名称的类(有意)

c++ - 如何在 C++ 中扩展和使用父类(super class)的成员?

oop - 设计WCF服务接口(interface)。过程式设计与面向对象设计

c# - 在 C# 中创建接口(interface)实例

javascript - 绑定(bind)页面高度变化的事件