Js 定义对象的方法

原文:http://yiminghe.iteye.com/blog/696296

参考:http://www.cnblogs.com/yugege/p/4823863.html 

 

里面的示例代码 有误。应该如下:

var lost = { 
     loc : "Island", 
     location : function () { return this.loc; }, 
     location :function (val) { this.loc = val; } 
}; 
lost.location = "Another island";

 

另外, IE8下 Object.defineProperty 定义到 String.prototype 上是报错的。

兼容一下:

jv.defineProperty = function (target, functionName, func, isEnumerable) {
    if (target[functionName]) return;
    if ($.browser.msie && (parseInt($.browser.version) < 9)) {
        target[functionName] = func;
    }
    else {
        Object.defineProperty(target, functionName, { value: func, enumerable: isEnumerable });
    }
}

 

IE8,不支持对 Object.prototype上进行定义: http://www.cnblogs.com/_franky/archive/2011/04/27/2030766.html

 

posted @ 2012-04-17 23:51  NewSea  阅读(2884)  评论(0编辑  收藏  举报