简体   繁体   English

在document.location上的Object.observe

[英]Object.observe on document.location

I need to execute a js script when the hash of the page changes, I know there are many ways to know when the hash changes like using jQuery 当页面的哈希值更改时,我需要执行一个js脚本,我知道有很多方法可以知道哈希值何时更改,例如使用jQuery。

$(window).on('hashchange', function() {
   // do something
});

I tried to use the Object.observe but it didn't work, I just whant to know why it didn't, why the callback doesn't fire after updating the document.location object. 我尝试使用Object.observe但是它不起作用,我只是想知道为什么不这样做,为什么在更新document.location对象后不触发回调。

Object.observe(document.location, function(changes) { 
    console.log(changes);
});

document.location along with many other DOM objects fall into the category of host objects which per spec do not have to behave like regular native JS objects. document.location以及许多其他DOM对象属于宿主对象的类别,根据规范,宿主对象的行为不必像常规的本机JS对象。 While Object.observe may work for some of these objects, it does not behave reliably and happens not to work for document.location. 尽管Object.observe可能适用于其中的某些对象,但它的行为不可靠,并且碰巧不适用于document.location。

Another example where it does not work is: 另一个不起作用的示例是:

 var el = document.createElement('div'); document.body.appendChild(el); Object.observe(el, function(change) { console.log('changed ', change[0].name); }) el.id = "hello"; el.foo = "bar"; 

I believe the reasons you are not able to observe document.location using Object.observe() is because document.location returns an object of type Location Object (which is special read-only interface) and not "Standard" Object. 我相信您无法使用Object.observe()观察document.location的原因是因为document.location返回的对象类型为Location Object (这是特殊的只读接口),而不是“ Standard” Object。

From Mozilla docs: 来自Mozilla文档:

The Document.location read-only property returns a Location object. Document.location只读属性返回一个Location对象。 The location property of the Document object refers to the Location object. Document对象的location属性引用Location对象。 Window.location is a read-only Location object. Window.location是一个只读的Location对象。

Location interface: https://developer.mozilla.org/en-US/docs/Web/API/Location 位置界面: https : //developer.mozilla.org/en-US/docs/Web/API/Location

Example: 例:

console.log(document.location.__proto__); // returns Location {}
console.log(window.location.__proto__);   // returns Location {}
window.location === document.location // always true

The Object.observe() method is used for observing the changes to an object. Object.observe()方法用于观察对象的更改。

Example: 例:

var o = { name: ''};
Object.observe(o, function(changes){
    changes.forEach(function(change) {
    console.log(change.type, change.name, change.oldValue);
});
});
o.name = 'foo'; // name is being observed

Look at the difference in their __proto__ 看看他们的__proto__的区别

console.log(document.location.__proto__); // returns Location {}
console.log(window.location.__proto__);   // returns Location {}
console.log(o.__proto__);                 // returns Object {}

Interesting enough if you test whether an object has in its prototype chain the prototype property of a constructor you can get: 如果您测试一个对象的原型链中是否具有构造函数的prototype属性,就足够有趣了:

console.log(document.location instanceof Object); // true
console.log(o instanceof Object); // true

Code sample: 代码示例:

 var o = { name: '' }; Object.observe(o, function(changes) { changes.forEach(function(change) { console.log(change.type, change.name, change.oldValue); }); }); o.name = 'foo'; console.log(document.location.__proto__); // returns Location {} console.log(window.location.__proto__); // returns Location {} console.log(o.__proto__); // returns Object {} console.log(document.location instanceof Object); // true console.log(o instanceof Object); // true 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM