javascript - Adding custom variables to an object -
this question has answer here:
suppose have object obj has variables in it. want add new variable has name gave parameter. example:
var obj= { number:8 } function addfield(field_name,obj) { //some magical code here }
if call
addfield('name',obj);
i want able do
obj.name='apple'
after calling function.
any ideas?
use square brackets notation:
obj[field_name] = value;
ref: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vid
Comments
Post a Comment