Skip to content

Commit fb0513f

Browse files
committed
Merge pull request GitbookIO#49 from Hermanya/master
Objects chapter, closes GitbookIO#11
2 parents a28a5d8 + b1b47fb commit fb0513f

File tree

11 files changed

+194
-1
lines changed

11 files changed

+194
-1
lines changed

SUMMARY.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@
2424
* [Functions](functions/README.md)
2525
* [Declare](functions/declare.md)
2626
* [Higher order](functions/higher_order.md)
27+
* [Objects](objects/README.md)
28+
* [Creation](objects/creation.md)
29+
* [Properties](objects/properties.md)
30+
* [Mutable](objects/mutable.md)
31+
* [Reference](objects/reference.md)
32+
* [Prototype](objects/prototype.md)
33+
* [Delete](objects/delete.md)
34+
* [Enumeration](objects/enumeration.md)
35+
* [Global footprint](objects/global_footprint.md)
36+

loops/for.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ for(var i = 0; i < 10; i = i + 1){
1616
}
1717
```
1818

19-
**Note**: `i = i + 1` can be written `i++`.
19+
>***Note***: `i = i + 1` can be written `i++`.
2020
2121

2222
---

objects/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Objects
2+
The primitive types of JavaScript are `true`, `false`, numbers, strings, `null` and `undefined`. **Every other value in an `object`.**
3+
4+
In JavaScript objects contain `propertyName`: `propertyValue` pairs.

objects/creation.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Creation
2+
There are two ways to create an `object` in JavaScript:
3+
4+
1. literal
5+
6+
```js
7+
var object = {};
8+
// Yes, simply a pair of curly braces!
9+
10+
```
11+
> ***Note:*** this is the **recomended** way.
12+
13+
2. and object-oriented
14+
15+
```js
16+
var object = new Object();
17+
18+
```
19+
> ***Note:*** it's almost like Java.

objects/delete.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Delete
2+
`delete` can be used to **remove a property** from an object. It will remove a property from the
3+
object if it has one. It will not look further in the prototype chain.
4+
Removing a property from an object may allow a property from the prototype chain to shine through:
5+
```js
6+
var adult = {age:26},
7+
child = Object.create(adult);
8+
child.age = 8;
9+
10+
delete child.age;
11+
/* Remove age property from child, revealing the age of the prototype, because then it is not overriden. */
12+
var prototypeAge = child.age;
13+
// 26, because child does not have its own age property.
14+
```

objects/enumeration.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Enumeration
2+
The `for in` statement can loop over all of the property names in an object. The enumeration will include functions and prototype properties.
3+
```js
4+
var fruit = {
5+
apple: 2,
6+
orange:5,
7+
pear:1
8+
},
9+
sentence = 'I have ',
10+
quantity;
11+
for (kind in fruit){
12+
quantity = fruit[kind];
13+
sentence += quantity+' '+kind+
14+
(quantity===1?'':'s')+
15+
', ';
16+
}
17+
// The following line removes the trailing coma.
18+
sentence = sentence.substr(0,sentence.length-2)+'.';
19+
// I have 2 apples, 5 oranges, 1 pear.
20+
```

objects/global_footprint.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Global footprint
2+
If you are developing an module, which might be running on a web page, which also runs other mudules, then you must beware the variable name overlapping.
3+
4+
Suppose we are developing a counter module:
5+
```js
6+
var myCounter = {
7+
number = 0,
8+
plusPlus = function(){
9+
this.number = this.number + 1;
10+
},
11+
isGreaterThanTen = function(){
12+
return this.number > 10;
13+
}
14+
}
15+
```
16+
> ***Note:*** this technique is often used with clusures, to make the internal state immutable from the outside.
17+
18+
The module now takes only one variable name — `myCounter`. If any other module on the page makes use of such names like `number` or `isGreaterThanTen` then it's perfectly safe, because we will not override each others values;

objects/mutable.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Mutable
2+
The difference between objects and primitive values is that **we can change objects**, whereas primitive values are immutable.
3+
```js
4+
var myPrimitive = "first value";
5+
myPrimitive = "another value";
6+
// myPrimitive now points to another string.
7+
var myObject = { key: "first value"};
8+
myObject.key = "another value";
9+
// myObject points to the same object.
10+
```

objects/properties.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Properties
2+
Object's property is a `propertyName`: `propertyValue` pair, where **property name can be only a string**. If it's not a string, it gets casted into a string. You can specify properties **when creating** an object **or later**. There may be zero or more properties separated by commas.
3+
```js
4+
var language = {
5+
name: 'JavaScript',
6+
isSupportedByBrowsers: true,
7+
createdIn: 1995,
8+
author:{
9+
firstName: 'Brendan',
10+
lastName: 'Eich'
11+
},
12+
// Yes, objects can be nested!
13+
getAuthorFullName: function(){
14+
return this.author.firstName + this.author.lastName;
15+
}
16+
// Yes, functions can be values too!
17+
};
18+
19+
```
20+
The following code demonstates how to **get** a property's value.
21+
```js
22+
var variable = language.name;
23+
// variable now contains "JavaScript" string.
24+
variable = language['name'];
25+
/* The lines above do the same thing. The difference is that the second one lets you use litteraly any string as a property name, but it's less readable. */
26+
variable = language.newProperty;
27+
// variable is now undefined, because we have not assigned this property yet.
28+
```
29+
The following example shows how to **add** a new property **or change** an existing one.
30+
```js
31+
language.newProperty = 'new value';
32+
// Now the object has a new property. If the property already exists, its value will be replaced.
33+
language['newProperty'] = 'changed value';
34+
// Once again, you can access properties both ways. The first one (dot notation) is recomended.
35+
```

objects/prototype.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Prototype
2+
Every object is linked to a prototype object from which it inherits properties.
3+
4+
All objects created from object literals (`{}`) are automatically linked to Object.prototype, which is an object that comes standard with JavaScript.
5+
6+
When a JavaScript interpreter (a module in your browser) tries to find a property, which You want to retrieve, like in the following code:
7+
```js
8+
var adult = {age: 26},
9+
retrievedProperty = adult.age;
10+
// The line above
11+
```
12+
Firsty, the interpreter looks through every property the object itself has. For example, `adult` has only one own property — `age`. But besides that one it actually has a few more properties, which were inherited from Object.prototype.
13+
```js
14+
var stringRepresentation = adult.toString();
15+
// the variable has value of '[object Object]'
16+
```
17+
18+
`toString` is an Object.prototype's property, which was inhereted. It has a value of a function, which returns a string representation of the object. If you want it to return a more meaningful representation, then you can `override` it. Simply add a new property to the adult object.
19+
20+
```js
21+
adult.toString = function(){
22+
return "I'm "+this.age;
23+
}
24+
```
25+
If you call the `toString` function now, the interpreter will find the new property in the object itself and stop.
26+
27+
Thus the interpreter retrieves the first property it will find on the way from the object itself and further through its prototype.
28+
29+
To set your own object as a prototype instead of the default Object.prototype, you can invoke `Object.create` as follows:
30+
31+
```js
32+
var child = Object.create(adult);
33+
/* This way of creating objects lets us easly replace the default Object.prototype with the one we want. In this case, the child's prototype is the adult object. */
34+
child.age = 8;
35+
/* Previously, child didn't have its own age property, and the interpreter had to look further to the child's prototype to find it.
36+
Now, when we set the child's own age, the interpereter will not go further.
37+
Note: adult's age is still 26. */
38+
var stringRepresentation = child.toString();
39+
// The value is "I'm 8".
40+
/* Note: we have not overrided the child's toString property, thus the adult's method will be invoked. If adult did not have toString property, then Object.prototype's toString method would be invoked, and we would get "[object Object]" instead of "I'm 8" */
41+
```
42+
43+
`child`'s prototype is `adult`, whose prototype is `Object.prototype`. This sequence of prototypes is called **prototype chain**.

0 commit comments

Comments
 (0)