JavaScript 跨浏览器Getters / Setters(包括IE)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 跨浏览器Getters / Setters(包括IE)相关的知识,希望对你有一定的参考价值。

/*
@name Xccessors
@version 0.0.3
@desc Shim that implements __defineGetter__, __defineSetter__, __lookupGetter__, and __lookupSetter__ in browsers that have ECMAScript 3.1 accessor support but not the legacy methods
@license http://www.gnu.org/licenses/lgpl.html
@author Elijah Grey - eligrey.com
*/
(function (methods, o, f) {
	function extendMethod(reqMethod, method, fun) {
		if (reqMethod in o && !(method in {})) o[f][method] = Element[f][method] = Window[f][method] = HTMLDocument[f][method] = fun;
	};
	extendMethod(methods[0], methods[2], function (prop, fun) {
		o[methods[0]](this, prop, { get: fun });
	});
	extendMethod(methods[0], methods[3], function (prop, fun) {
		o[methods[0]](this, prop, { set: fun });
	});
	extendMethod(methods[1], methods[4], function (prop) {
		return o[methods[1]](this, prop).get
		|| o[methods[1]](this.constructor.prototype, prop).get;
	});
	extendMethod(methods[1], methods[5], function (prop) {
		return o[methods[1]](this, prop).set
		|| o[methods[1]](this.constructor.prototype, prop).set;
	});
})(["defineProperty", "getOwnPropertyDescriptor","__defineGetter__","__defineSetter__","__lookupGetter__","__lookupSetter__"], Object, "prototype");

以上是关于JavaScript 跨浏览器Getters / Setters(包括IE)的主要内容,如果未能解决你的问题,请参考以下文章

封装常用的Javascript跨浏览器方法

跨浏览器 Javascript XML 解析

PerformanceNavigation 的替代 Javascript 方法(跨浏览器兼容)

跨浏览器书签/添加到收藏夹 JavaScript [重复]

跨平台、跨浏览器的 Javascript 性能分析

跨浏览器 JavaScript(不是 jQuery...)滚动到顶部动画