是否有可能创建一个更具动态性的新类型? [英] Is it possible to create a new type that is more dynamic?

查看:75
本文介绍了是否有可能创建一个更具动态性的新类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能不是正确的词。但我想在JavaScript中创建一个新类型。它有一个简单的属性,人们可以这样做:

Probably not the correct word. But I want to create a new type in JavaScript. It would have the simple property that one could do this:

var inst = new SomeType();
inst.key1.key2 = 'something';
inst.key1.key1.key3 = 'something';

基本上你不必声明一个对象文字来进一步扩展。它会自动创建一个。

Basically you wouldn't have to declare an object literal to extend further. It would create one automatically.

这将允许我构建复杂的结构,而不必担心检查是否存在要延伸的属性。

This would allow me to build complex structures without having to worry about checking for the existence of a property to extend off of.

而不是

inst.key1 = {};

inst.key1.key2 = 'data';

一个人可以做到

inst.key1.key2 = 'data';

inst.key1 = {};

将是自动的,即在内部发生。

would be automatic, i.e. would happen internally.

这确实有实际意义。特别是我有一个注册表模式,我将使用这种新类型来使用更分层的方法来组织数据。

This does have a practical purpose. Particularly I have a registry pattern which I would use this new type to organize data using a more hierarchical approach.

此外,我看到一个模式,在库中常见,测试对于文本的存在,如果它不存在则创建一个。

Also, I see a pattern, common in libraries, that tests for the existence of an object literal and then creates one if it does not exist.

这是一个常见的习语。

推荐答案

使用和声代理 MDN )。 但是,此API尚未稳定,因此可以在非关键业余爱好代码中使用它,但不要在生产代码中使用它(还有)。

What you'd like to get can easily be achieved with Harmony proxies (MDN). However, this API is not yet stable, so it's fine to use it in non-critical hobby code, but don't use it in production code (yet).

这是一个非常简单的例子:

Here's an extremely simple example:

function getFreeChain(object) {
    var handler = {
        get: function(target, name) {
            if (name in target)
                return target[name];
            var newTarget = target[name] = {};
            return new Proxy(newTarget, handler);
        }
    };
    return new Proxy(object, handler);
}

// Usage:
var obj = {};
var magicalObj = getFreeChain(obj);
magicalObj.a.b.c.d.e.g = 1;
console.log(magicalObj.a.b.c.d.e.g); // 1
console.log(obj.a.b.c.d.e.g);        // 1
obj.x.y.z = 1;                       // TypeError: obj.x is undefined

注意:我的示例是最新<$的实现c $ c>代理规范,仅受Firefox支持。
Chrome仅支持旧的,显着不同的API版本,可以通过打开 chrome:// flags / 上的实验性JavaScript来启用。这个旧的API很难看,实现之前需要更多的代码行,所以我将把它作为练习留给读者。

Note: My example is an implementation of the latest Proxy specification, which is only supported by Firefox . Chrome only supports an old, significantly different version of the API, which can be enabled by turning on "Experimental JavaScript" at chrome://flags/. This old API is ugly, and implementing the previous would require significantly more lines of code, so I'll leave this as an exercise to the reader.

哦,有一个名为 DirectProxies的库.js (由和谐反映取代)将简单的Proxy API引入Chrome。包含此库后,以前的代码将在Firefox和Chrome中运行(启用实验): http://jsfiddle.net / PAhYL /

Oh, there's a library called DirectProxies.js (superseded by harmony-reflect) which brings the simple Proxy API to Chrome. After including the this library, the previous code will work in Firefox and Chrome (with experiments enabled): http://jsfiddle.net/PAhYL/

这篇关于是否有可能创建一个更具动态性的新类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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