I was looking at http://docs.jquery.com/Plugins/Authoring#Defaults_and_Options to create a simple plugin for jQuery. Following the section about options and settings, I did the following, which didn't work (the script quit when it encountered the setting).
var settings = { 'location' : 'top', 'background-color': 'blue' } ... $this.css('backgroundColor', settings.background-color); // fails here
Once I removed the dash from the background color, things work properly.
var settings = { 'location' : 'top', 'backgroundColor': 'blue' // dash removed here } ... $this.css('backgroundColor', settings.backgroundColor);
Am I missing something, or are the jQuery docs wrong?
Vars with dashes are invalid in js, so if we'll compile it with mangling, it won't violate js naming style.
Hyphens are used as subtraction and negation operators, so they cannot be used in variable names.
Use bracket notation to access an object property with a hyphen, e.g. obj['with-hyphen'] . There are two ways to access properties on an object - dot notation and bracket notation. If the property contains a hyphen, space, or special symbols, you have to use bracket notation.
The function name property of the javascript object is used to return the name of the function. This name property of the function is only readable and cannot be altered. The name of the function which was given when the function was created is returned by Function.name.
no. the parser will interpret it as the subtract operator.
you can do settings['background-color']
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With