Java script prototype_hack

100 %
0 %
Information about Java script prototype_hack
Technology

Published on February 24, 2014

Author: ltcrd

Source: slideshare.net

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/enUS/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 2 function MyClass(){}      var a = new MyClass();      MyClass.prototype.hello = function(){          console.log('hello 1');      }      a.hello(); 

Sample 3 function 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 在不帶參數時,並不是在最後加入的差 異 var oTable = document.getElementById("InventorItem"); var oRow = oTable.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);      };  } 

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

Add a comment

Related presentations

Related pages

Ditch the [].forEach.call(NodeList) hack - Todd Motto

Ditch the [].forEach.call(NodeList) hack ... But that’s yet another Array.prototype hack, ... (usually seen at the end of a script) ...
Read more

Declare static non-method members within class' prototype ...

Declare static non-method members within class' prototype #3743. ... And it's irrelevant for the prototype hack workaround I did. ... (as e.g. in Java).
Read more

Prototype.js, Form.serialize(), submit button names, and ...

> Yeah I was leaning towards a prototype hack, ... a> >> >>