javascript - Is it possible to pass object as parameter? -
var obj = { name1: 1, name2: 2 } function myf(obj) { console.log(obj.name1) // idea must return 1 }; myf(obj)
does know how pass object in function?
yes objects make great parameters.
var p1 = { name: "tom", age: 23, ismale: true }; var p2 = { name: "alicia", age: 21, ismale: false }; var p3 = { name: "landon", age: 1, ismale: true }; function greeting(person) { var str = ''; str += 'hello name '; str += person.name + ' '; str += 'i'm ' + person.age + ' year old '; if (person.ismale) { str += age > 18 ? 'man' : 'boy'; } else { str += age > 18 ? 'woman' : 'girl'; } if (person.age < 3) { str = 'bah' } console.log(str); }; greeting(p1); // 'hello name tom i'm 23 year old man'; greeting(p2); // 'hello name alicia i'm 21 year old woman; greeting(p3); // 'bah';
objects when have grouping of values belong , don't want pass them in individually (if belong should passed on own.)
Comments
Post a Comment