Java script prototype_hack

14
JavaScript prototype & hack By Jax

Transcript of Java script prototype_hack

Page 1: Java script prototype_hack

JavaScript prototype & hack

By Jax

Page 2: Java script prototype_hack

Overview

• Object.prototype• Global Objects• DOM interfaces• EPS Hack Sample

Page 3: Java script prototype_hack

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

Page 4: Java script prototype_hack

Sample 1

function MyClass(){}     

MyClass.prototype.hello = function(){         

console.log('hello 1');     }     

var a = new MyClass();     

a.hello();

Page 5: Java script prototype_hack

Sample 2function MyClass(){}     

var a = new MyClass();     

MyClass.prototype.hello = function(){         

console.log('hello 1');     }     

a.hello();

Page 6: Java script prototype_hack

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();

Page 7: Java script prototype_hack

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();

Page 8: Java script prototype_hack

Global Objects• Object• Function• Boolean• Number• Math• Date• Array• String• RegExp• ...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference

Page 9: Java script prototype_hack

DOM interfaces• Element• Event• HTMLElement• HTMLFrameElement• HTMLFormElement• HTMLTableElement• HTMLTableRowElement• HTMLTableCellElement• ...

https://developer.mozilla.org/en-US/docs/DOM/DOM_Reference

Page 10: Java script prototype_hack

以 XMLHttpRequest 偽裝 ActiveXObject

if (typeof (window.ActiveXObject) == "undefined" && window['XMLHttpRequest']) {     

window.ActiveXObject = function () {         return new XMLHttpRequest();     

}; }

Page 11: Java script prototype_hack

以 addEventListener 偽裝 attachEvent

div.attachEvent(“onclick", function(){

});

if (typeof(window.attachEvent) == "undefined") {     window.attachEvent = function (event, pDisp) {         

this.addEventListener(event.substr(2), pDisp, false);     

}; }

Page 12: Java script prototype_hack

覆寫 getElementById

document.org_getElementById = document.getElementById;

document.getElementById = function (id) {     var el = document.org_getElementById(id);

    // ...     

return el; }

Page 13: Java script prototype_hack

修正 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();

Page 14: Java script prototype_hack

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(); /*遞迴呼叫 */ }