有没有办法从某人更改Object.prototype.hasOwnProperty中恢复
Is there any way to recover from somebody changing Object.prototype.hasOwnProperty?
你总是读到for-in循环应该检查o.hasOwnProperty(k)
跳过Object.prototype。好吧,如果有人愚蠢到修改Object.prototype,谁能说他们不会做任何与现有原型冲突的事情呢?例如,如果有人运行以下命令怎么办:
Object.prototype.hasOwnProperty = function () {
return !!'I am stupid';
};
如果这是第一次运行脚本,这是否意味着,对于页面其余部分的每个脚本,实际上不可能安全地迭代对象?
您可以创建一个临时 IFRAME 元素,并从其Object.prototype
对象中检索该方法:
(function () {
var frame = document.createElement( 'iframe' );
frame.style.display = 'none';
document.body.appendChild( frame );
Object.prototype.hasOwnProperty = frame.contentWindow.Object.prototype.hasOwnProperty;
document.body.removeChild( frame );
}());
现场演示:http://jsfiddle.net/ARycC/2/
function secret(){
var HOP = Object.prototype.hasOwnProperty;
this.getHOP = function(){
return HOP;
}
}
new secret().getHOP(); //Will now be the copy of hasOwnProperty
//and nobody can modify it
//stupid script:
Object.prototype.hasOwnProperty = function () {
return !!'I am stupid';
};
//restore
Object.prototype.hasOwnProperty = new secret().getHOP(); //DONE.
更新
如果在加载每个脚本之前对其进行修改,则可以执行以下操作:
Object.prototype.hasOwnProperty = String.prototype.hasOwnProperty
它们是一样的。 :D
还有更多内容可供您复制:
-
Boolean.prototype.hasOwnProperty
-
Number.prototype.hasOwnProperty
-
Function.prototype.hasOwnProperty
- 等。。。
更新 2
恢复原始功能的新方法:
var win=window.open("about:blank");
win.close();
Object.prototype.hasOwnProperty = win.Object.prototype.hasOwnProperty;
有点笨拙。或者您可以创建一个隐藏<iframe>
。
var win = document.createElement("iframe");
win.style.display = "none";
document.body.appendChild(win);
Object.prototype.hasOwnProperty = win.contentWindow.Object.prototype.hasOwnProperty;
document.body.removeChild(win);
如果有人覆盖它,不丢失它的唯一方法是在有人覆盖它之前保留自己的引用。
因此,在包含任何其他脚本之前,请包含以下脚本
var hasOwnProp = Object.prototype.hasOwnProperty;
并在其他任何地方使用hasOwnProp
。
对我来说
,这不是一个问题。您在帖子和评论中声明:
- 这是假设的
- 它基于这样一个事实,即"某人"可以修改您的脚本,而您对此无能为力。
- 您对任何涉及控制脚本加载顺序的解决方案都不满意。
为什么不呢?你不是开发人员吗?如果"某人"确实修改了Object.prototype.hasOwnProperty
,解决方案非常简单:从页面中删除有问题的脚本!这不像是有人闯入你的代码并这样做,而是你链接到某人写得不好的脚本。
PS:如果这不是您问题的实际答案,我很抱歉,但是评论太长了。
相关文章:
- Object.prototype using 'this'
- ExtJS 4 Object.prototype fail
- Object.prototype.hasOwnProperty.call() vs Object.prototype.h
- 为什么使用 Object.prototype.hasOwnProperty.call(myObj, prop) 而不是
- childObj.prototype = Object.create(parentObj.prototype) 和 ch
- 如何添加 Object.prototype.x?设置为 enumerable:false
- Object.create() instead of prototype
- 为什么 Object.prototype 和 Object.getOwnPropertyNames(Object.pro
- 引用 Object.prototype.toString.call 导致“TypeError: undefined 不是
- 为什么MDN Object.create polyfill上的Temp.prototype设置为null
- Function.prototype.propertyname === Object.propertyname is t
- Object.seal(Object.prototype)是否使所有对象不可变?
- Object.create Prototype Chains
- Object.Prototype 方法和 IIFE(立即调用的函数表达式)中的“use strict”
- Object.prototype.values 会破坏应用程序
- object.key = fn() 与 object.prototype.key = fn() 之间的差异
- Javascript Prototype Object Constructor
- 从prototype.object获取类上下文
- Rectangle.prototype=Object.create(Shape.prototype)和Rectangle
- 如何绑定'这'prototype.object上的上下文正确