ECMA V5时代后,我们能玩的东西

当V5正式降临的时候,我们能玩的就多了。看看吧:

玩转times

Object.defineProperty(Number.prototype, 'times', {
    get: function() {
        var n = this;
        return function(f) {
            for (var i = 0; i < n; i++) f()
        }
    }
})

和扩展方法不同,这里不仅能使用5.times(someAction),还可以直接把这个进行重复的函数保存下来:

var n = STDIN();
var repeat = n.times;
repeat(someAction);

DOM代理

DOM代理将会和原生DOM对象一样,你将看不出一点突兀

ps. 要是都支持ECMA V5了还需要代理吗?
DOMElementProxy.value = {
    get: function() {
    /*...*/
    },
    set: function(newval) {
    /*...*/
    },
    configurable: false
}

// query and set
$('field').value = 'sometext'

常量表

V3时代,我们必须手工维护常量表保证不会被改;而V5时代,一个freeze就搞定了:

Constants.U = "p";
Constants.B = "n";
Constants.S = "f";
Object.freeze(Constants)

在非Strict的环境下,任何尝试对Constants的写操作都会被忽略;而Strict环境下则会直接报异常。

Strict环境

在一个函数的开始加一个字符串即可开启strict环境。所有位于strict环境中的函数也同样位于strict环境:

function() {
    "use strict"
    /* code */
}
This entry was posted in Browser, 中文 and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>