-
Notifications
You must be signed in to change notification settings - Fork 558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Objects: Change foobar to slightly better variables #373
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,9 @@ Everything in JavaScript acts like an object, with the only two exceptions being | |
false.toString(); // 'false' | ||
[1, 2, 3].toString(); // '1,2,3' | ||
|
||
function Foo(){} | ||
Foo.bar = 1; | ||
Foo.bar; // 1 | ||
function sayHello(){} | ||
sayHello.bar = 1; | ||
sayHello.bar; // 1 | ||
|
||
A common misconception is that number literals cannot be used as | ||
objects. That is because a flaw in JavaScript's parser tries to parse the *dot | ||
|
@@ -32,25 +32,25 @@ Using an object literal - `{}` notation - it is possible to create a | |
plain object. This new object [inherits](#object.prototype) from `Object.prototype` and | ||
does not have [own properties](#object.hasownproperty) defined. | ||
|
||
var foo = {}; // a new empty object | ||
var names = {}; // a new empty object | ||
|
||
// a new object with a 'test' property with value 12 | ||
var bar = {test: 12}; | ||
// a new object with a 'name' property with value 'Rob' | ||
var rob = {name: 'Rob'}; | ||
|
||
### Accessing Properties | ||
|
||
The properties of an object can be accessed in two ways, via either the dot | ||
notation or the square bracket notation. | ||
|
||
var foo = {name: 'kitten'} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha - this is a great change. I wonder y someone decided on 'kitten' and stuck with foo as the variable name :D |
||
foo.name; // kitten | ||
foo['name']; // kitten | ||
var pet = {name: 'kitten'} | ||
pet.name; // kitten | ||
pet['name']; // kitten | ||
|
||
var get = 'name'; | ||
foo[get]; // kitten | ||
pet[get]; // kitten | ||
|
||
foo.1234; // SyntaxError | ||
foo['1234']; // works | ||
pet.1234; // SyntaxError | ||
pet['1234']; // works | ||
|
||
The notations work almost identically, with the only difference being that the | ||
square bracket notation allows for dynamic setting of properties and | ||
|
@@ -63,21 +63,21 @@ operator; setting the property to `undefined` or `null` only removes the | |
*value* associated with the property, but not the *key*. | ||
|
||
var obj = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm less keen on this change. If we're not replacing 'foo' with something meaningful & realisitic, I think foo, bar, baz is actually preferable to 'a','b','c'. More people will realise it's an explicit 'name does not matter' placeholder. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that was the exact reason in my head to make it abc. Because of this comment: #261 (comment)
foo, bar, etc. might seem like they're supposed to make sense. abc are alphabets as placeholder 😅 |
||
bar: 1, | ||
foo: 2, | ||
baz: 3 | ||
a: 1, | ||
b: 2, | ||
c: 3 | ||
}; | ||
obj.bar = undefined; | ||
obj.foo = null; | ||
delete obj.baz; | ||
obj.a = undefined; | ||
obj.b = null; | ||
delete obj.c; | ||
|
||
for(var i in obj) { | ||
if (obj.hasOwnProperty(i)) { | ||
console.log(i, '' + obj[i]); | ||
} | ||
} | ||
|
||
The above outputs both `bar undefined` and `foo null` - only `baz` was | ||
The above outputs both `a undefined` and `b null` - only `c` was | ||
removed and is therefore missing from the output. | ||
|
||
### Notation of Keys | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you missed a 'bar' :)