Java script prototype_hack
-
Upload
learningtech -
Category
Technology
-
view
288 -
download
1
Transcript of Java script prototype_hack
JavaScript prototype & hack
By Jax
Overview
• Object.prototype• Global Objects• DOM interfaces• EPS Hack Sample
Object.prototype
All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden (except an Object with a null prototype, i.e. Object.create(null)). For example, other constructors' prototypes override the constructor property and provide their own toString methods. Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype
Sample 1
function MyClass(){}
MyClass.prototype.hello = function(){
console.log('hello 1'); }
var a = new MyClass();
a.hello();
Sample 2function MyClass(){}
var a = new MyClass();
MyClass.prototype.hello = function(){
console.log('hello 1'); }
a.hello();
Sample 3function MyClass(){}
MyClass.prototype.hello = function(){
console.log('hello 1'); }
var a = new MyClass();
var b = new MyClass();
a.hello = function(){ console.log('hello 2');
};
b.hello();
Class Inherit var Person = function() {
this.greet = function() { console.log("Hi, I'm " + this.name);
};
};
var Customer = function(name) { this.name = name;
}; Customer.prototype = new Person();
var joe = new Customer('Joe'); var mike = new Customer('Mike'); joe.greet(); mike.greet();
Global Objects• Object• Function• Boolean• Number• Math• Date• Array• String• RegExp• ...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
DOM interfaces• Element• Event• HTMLElement• HTMLFrameElement• HTMLFormElement• HTMLTableElement• HTMLTableRowElement• HTMLTableCellElement• ...
https://developer.mozilla.org/en-US/docs/DOM/DOM_Reference
以 XMLHttpRequest 偽裝 ActiveXObject
if (typeof (window.ActiveXObject) == "undefined" && window['XMLHttpRequest']) {
window.ActiveXObject = function () { return new XMLHttpRequest();
}; }
以 addEventListener 偽裝 attachEvent
div.attachEvent(“onclick", function(){
});
if (typeof(window.attachEvent) == "undefined") { window.attachEvent = function (event, pDisp) {
this.addEventListener(event.substr(2), pDisp, false);
}; }
覆寫 getElementById
document.org_getElementById = document.getElementById;
document.getElementById = function (id) { var el = document.org_getElementById(id);
// ...
return el; }
修正 insertRow 在不帶參數時,並不是在最後加入的差異
if (window['HTMLTableElement']) {
HTMLTableElement.prototype.org_insertRow = HTMLTableElement.prototype.insertRow;
HTMLTableElement.prototype.insertRow = function (index) { if (!index) { index = this.rows.length; } return this.org_insertRow(index);
}; }
var oTable = document.getElementById("InventorItem");var oRow = oTable.insertRow();
Resize Parent Iframefunction parentResize() {
if(window.parent == window || !window.parent['_resizeIframe']){ return; }
window.parent._resizeIframe(window, $(document).width(), $(document).height()); }
function _resizeIframe (childWindow, width, height) { var $iframe = $('iframe').filter(function(){
return this.contentWindow == childWindow; });
$iframe.css({'width':width, 'height':height});
parentResize(); /*遞迴呼叫 */ }