(30 === What Are Objects For?) {Does the space between "Object ()" really matter?"}


#1

Just curious about this.
At first i had a space better the constructor Object and the parameter, and the code wouldn't run.
I removed the space, and it ran.
Does the space make any real difference, and if so why? I thought things like that didn't matter.

//CODE W/OUT SPACE
var spencer2 = new Object();
spencer2.age = 22;
spencer2["country"] = "United States";

//CODE W/ SPACE
var spencer2 = new Object ();
spencer2.age = 22;
spencer2["country"] = "United States";


#2

@bassandsynths,

IMHO
this is called SYNTAX.

An object has one or more properties, separated by a comma_,

A property has a property-key and it's associated VALUE

The VALUE can be anything from:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         ◦Array
         ◦Date
         ◦RegExp
         ◦Math
•Null
•Undefined

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object


#3

@bassandsynths,

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <<< Javascript code-convention !!!
http://javascript.crockford.com/survey.html
http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1


#4

Hi.

Thanks for the reply. Honestly though I think the explanation is a little over my head.
I'm going to try and understand it though ^_^

Thanks again.


#5

@bassandsynths this code works


#6

It's simply that adding a space makes the syntax incorrect, it's a build in JS constructor which would need to be the exact correct layout, so the program would read new Object(); as matching up with the correct syntax. Adding the space then it doesn't match up with what JavaScript is expecting.


#7

I'm so annoyed. I got stung by this too. I always write my code with a space between Object and ()