javascript - What is the different between these -
i have been using java script , jquery sometime. still couldn't figure out proper definitions of below declaration of java script
var person = function(){ name = "person1"; age ="24" } function person (){ name = "perlson1"; age ="24"; } var person = { name : "person1", age : "24", } could give me proper explanation these 3 type,i bit confused way implementing within project,
i appreciate it
var person = function(){ name = "person1"; age ="24" } the above code defines variable 'person', assigned anonymous function sets values 2 global variables 'name' , 'age'.
function person (){ name = "perlson1"; age ="24"; } the above code defines function sets values 2 global variables 'name' , 'age'. so, syntactically 1st & 2nd different way of defining function. but, serve same purpose. access them, console.log(name); //or console.log(window.name); console.log(age); //or console.log(window.age);
var person = { name : "person1", age : "24", } the above 3rd code block object. cannot called other code. defines object person, has 2 properties 'name' , 'age'. note here, 2 properties - name , age belong person object , not window. i.e. not global variables. bound person object. access these, console.log(person.name); console.log(person.age);
Comments
Post a Comment