javascript - Adding custom variables to an object -


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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -