javascript - what effect does the 'new' have in a JS method? -
this question has answer here:
- what 'new' keyword in javascript? 11 answers
what difference between these 2 methods?
function objectb() { this.methoda = new function() { alert('a'); }; this.methodb = function() { alert('b'); }; } what trying ask, effect new have in js method?
i've done fiddle wanted explore behaviour of methods, , have added code :
var v = object.create(objectb); v.methodc = function() { alert('c'); } v.methodb(); v.methoda(); v.methodc(); but fiddle doesnt seem work.
fiddle here : http://jsfiddle.net/n8sng/
thanks :)
methoda not method, because new operator causes function after called constructor. object methoda anonymous function equivalent of class.
it's if had written this:
var methoda = function() { alert('a'); }; this.methoda = new methoda; and last line same this:
this.methoda = new methoda();
Comments
Post a Comment