Skip to content

Instantly share code, notes, and snippets.

@bga
Created March 25, 2010 12:15
Show Gist options
  • Save bga/343478 to your computer and use it in GitHub Desktop.
Save bga/343478 to your computer and use it in GitHub Desktop.
/* MODIFY PROTOTYPE */
var _fn=function(){};
_fn.prototype.a=1;
var v=new _fn();
alert(v.a === 1); /* true */
alert(v.constructor === _fn); /* true */
/* REPLACE PROTOTYPE */
var _fn=function(){};
_fn.prototype={a:1};
var v=new _fn();
alert(v.a === 1); /* true */
alert(v.constructor === _fn); /* false in ie; true in other browsers */
/* but */
alert(v.constructor === Object.prototype); /* true in ie. ie set 'constructor' property eq Object if 'prototype' replaced */
/* REPLACE PROTOTYPE WITH FIXING BUG OF IE */
var _fn=function(){};
_fn.prototype={a:1};
var v=new _fn();
v.constructor=_fn; /* set 'constructor' property by hands */
alert(v.a === 1); /* true */
alert(v.constructor === _fn); /* now true in all browsers */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment