jquery - javascript prototype constructor and instanceof -


when checked instanceof method, results not same .

function a(){} function b(){}; 

first assigned prototype ( reference ) property , a

a.prototype = b.prototype; var cara =  new a();  console.log( b.prototype.constructor ); console.log( a.prototype.constructor == b ); console.log( b.prototype.constructor == b ); console.log( cara  instanceof ); console.log( cara  instanceof b ); 

the last 4 condition on above returns true .

but when tried assign constructor of b .. results not same .

a.prototype.constructor = b.prototype.constructor; var cara =  new a();  console.log( b.prototype.constructor ); console.log( a.prototype.constructor == b ); console.log( b.prototype.constructor == b ); console.log( cara  instanceof ); console.log( cara  instanceof b ); 

on case cara instanceof b returns false . why returns false

i found answer link .. https://stackoverflow.com/a/12874372/1722625

instanceof checking internal [[prototype]] of left-hand object . same below

function _instanceof( obj , func ) {     while(true) {        obj = obj.__proto__; // [[prototype]] (hidden) property        if( obj == null) return false;        if( obj ==  func.prototype ) return true;     } }  // true  console.log( _instanceof(cara , b ) == ( obj instanceof b ) )  

if returns true, obj instanceof b


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -