javascript - what effect does the 'new' have in a JS method? -


this question has answer here:

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

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -