-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjects.js
More file actions
77 lines (72 loc) · 2.7 KB
/
Copy pathobjects.js
File metadata and controls
77 lines (72 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* In JS, everything is an object or typeof some object. Objects have properties(and methods). Some argue that JS has "native" data types like String, Number, Boolean and Null etc but they all have properties attached to them which make them behave like an object.
*/
var foo = 'FUBAR'; // a string object
console.log(foo.length); //strings have length property
console.log(foo.toLowerCase()); //can be converted into lower case using toLowerCase() method.
/*
* Properties in all JS objects have a descriptor field associated with them that tells that whether a property is
* - writable - is the property modifiable?
* - configurable - whether the property can be deleted from the object?
* - enumerable - is property "iterate"-able?
* More at: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty
*/
foo.length = 10; // This is wrong
console.log(foo.length); // String.length can not be modified, this is "illegal" :-|
/*
* Now, to confuse further, JS has objects of type Object. (And even typeof function;-)
*/
var bar = {'objType':'Chocolate bar'}; //this is known as JS Object literal, "they" say this is the best way to create an object, with an initial property.
var baz = new Object(); // creates an object of type Object.
/*
* Adding properties to an object.
*/
bar.type = 'dark chocolate';
bar['cocoa_level'] = '55%';
/*
* Properties can be accessed same way.
*/
console.log(bar.type);
console.log(bar['cocoa_level']);
/*
* or
*/
for(var property in bar){
console.log(bar[property]); // but bar.property will not work, why?
}
/*
* or
*/
for(var i=0; i < bar.length; i++){
console.log(bar[i]);
}
/*
* An object is a container of properties, where a property has a name and a value.
* A property name can be any string, including the empty string. A property value
* can be any JavaScript value except for undefined.
*
*/
//Object literals
var Person = {};
//Setting properties
Person.first_name = "John";
Person["last_name"] = "Doe";
//Getting properties
console.log("Like an array key " + Person["first_name"]);
console.log("Using a dot operator " + Person.last_name);
//Or using "this"
Person.getInfo = function(){
//this refers to "this" object which is Person "John Doe"
return (this.first_name + " "+ this.last_name);
};
console.log(Person.getInfo());
//Objects are passed around by reference. They are never copied.
var a = {}, b = {}, c = {};
// a, b, and c each refer to a different empty object
var d = e = f = {};
// d, e, and f all refer to the same empty object
a.some_property = "Limited to a";
d.some_property = "d, e and f will have same copy!";
console.log(a.some_property);
console.log(b.some_property);//undefined
console.log(e.some_property);