객체 속성을 확인하십시오.
-
13-09-2019 - |
문제
가능한 복제 :
JavaScript 객체. 모든 브라우저에 대한 워치?
방금 Mozilla의 문서를 읽었습니다 시계 () 메소드. 매우 유용 해 보입니다.
그러나 사파리와 비슷한 것을 찾을 수 없습니다. 인터넷 익스플로러도 아닙니다.
브라우저에서 휴대 성을 어떻게 관리합니까?
해결책
나는 작은 것을 만들었습니다 객체 이것에 대해 얼마 전에. IE8, Safari, Chrome, Firefox, Opera 등에서 작동합니다.
/*
* object.watch v0.0.1: Cross-browser object.watch
*
* By Elijah Grey, http://eligrey.com
*
* A shim that partially implements object.watch and object.unwatch
* in browsers that have accessor support.
*
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
// object.watch
if (!Object.prototype.watch)
Object.prototype.watch = function (prop, handler) {
var oldval = this[prop], newval = oldval,
getter = function () {
return newval;
},
setter = function (val) {
oldval = newval;
return newval = handler.call(this, prop, oldval, val);
};
if (delete this[prop]) { // can't watch constants
if (Object.defineProperty) // ECMAScript 5
Object.defineProperty(this, prop, {
get: getter,
set: setter
});
else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
Object.prototype.__defineGetter__.call(this, prop, getter);
Object.prototype.__defineSetter__.call(this, prop, setter);
}
}
};
// object.unwatch
if (!Object.prototype.unwatch)
Object.prototype.unwatch = function (prop) {
var val = this[prop];
delete this[prop]; // remove accessors
this[prop] = val;
};
다른 팁
불행히도 이것은 휴대용 솔루션이 아닙니다. 즉, 내 지식에는 이와 같은 것이 없습니다.
덮어 쓰기 방법과 변수를 통해 자신의 알림 시스템을 구현할 수 있습니다. 나는 그것이 그렇게 중요한 것으로 보지 않지만, 당신이 그러한 시스템으로 무엇을하고 있는지 모르겠습니다.
제휴하지 않습니다 StackOverflow