object = {}
key = 'jimbo'
$.extend( object, { key : 1 })
console.log( object ); // returned {key: 1}, expected {jimbo: 1}
It's strange. The variable is evaluated as a string, and that's used as the named key. I want the key to be called jimbo
, not key
.
I'm not sure what the advantages of $.extend are, but this works:
object[key]=1
The problem with your example is, as you noticed, {key: 1}, evaluates "key" as a string, not a variable. Similar behavior applies to
object.key = 1
versus
object[key] = 1
I've spent more time than I'd like to admit debugging this exact issue in my own code, particularly when I started out with javascript.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments