var o = {prop: 1};
var o = {"prop": 1};
var o = {'prop': 1};
There are some cases when you have must use quotes:
- If the property name is one of the reserved words in JavaScript
- If it contains spaces or special characters (anything other than letters, numbers, and the underscore character)
- If it starts with a number
If the name you have chosen for a property is not a valid name for a variable in JavaScript, then you need to in place in quotes
A property of an object can contain a function
var dog = {
name : 'Benji',
talk : function() {
alert('Woof, woof!');
}
};
JavaScript uses arrays to represent indexed arrays and objects to represent associative arrays.
Accessing Object's Properties
Using square bracket notation, for example hero['occupation']
Using the dot notation, for example hero.occupation
if the name of the property is not a valid variable name, you cannot use the dot notation.
Accessing a non-existing property returns undefined:
>>> 'Hair color is ' + hero.hair_color;
"Hair color is undefined"
Objects can contain any data, including other objects.
var book = {
name : 'Catch-22',
published : 1961,
author : {
firstname : 'Joseph',
lastname : 'Heller'
}
};
>>> book.author.firstname
>>>"Joseph"
>>> book['author']['lastname']
>>>"Heller"
>>> book['author'].lastname
>>>"Heller"
Calling an Object's Methods
var hero = {
breed : 'Turtle',
occupation : 'Ninja',
say : function() {
return 'I am ' + hero.occupation;
}
}
>>> hero.say();
>>> hero['say']();
Altering Properties/Methods
>>> var hero = {};
Adding some properties and a method
>>> hero.breed = 'turtle';
>>> hero.name = 'Leonardo';
>>> hero.sayName = function() {return hero.name;};
Using this Value
So when you say this, you are actually saying "this object" or "the current object".
var hero = {
name : 'Rafaelo',
sayName : function() {
return this.name;
}
}
>>> hero.sayName();
"Rafaelo"
Constructor Functions
There is another way to create objects: by using constructor functions
function Hero() {
this.occupation = 'Ninja';
}
>>> var hero = new Hero();
>>> hero.occupation;
"Ninja"
function Hero(name) {
this.name = name;
this.occupation = 'Ninja';
this.whoAreYou = function() {
return "I'm " + this.name + " and I'm a " + this.occupation;
}
}
>>> var h1 = new Hero('Michelangelo');
>>> var h2 = new Hero('Donatello');
>>> h1.whoAreYou();
The Global Object
The host environment provides a global object and all global variables are actually properties of the global object
the global object is called window
>>> var a = 1;
>>> window['a'] or window.a
1
The global functions you saw in Chapter 3 can also be invoked as methods of the window object.
>>> parseInt('101 dalmatians')
>>> window.parseInt('101 dalmatians')
Oddities
Declaring a constructor function and calling it without new, returns "undefined"
>>> function Hero(name) {this.name = name;}
>>> var h = Hero('Leonardo');
>>> typeof h
"undefined"
>>> typeof h.name
h has no properties
>>> name
"Leonardo"
>>> window.name
"Leonardo"
>>> var h2 = new Hero('Michelangelo');
>>> typeof h2
"object"
>>> h2.name
Michelangelo
constructor Property
When an object is created, a special property is assigned to it behind the scenes—the constructor property
It contains a reference to the constructor function used to create this object.
>>> h2.constructor
Hero(name)
>>> var h3 = new h2.constructor('Rafaello');
>>> h3.name;
Rafaello
If an object was created using the object literal notation, its constructor is the built-in Object() constructor function
>>> var o = {};
>>> o.constructor;
Object()
>>> typeof o.constructor
function
instanceof Operator
an test if an object was created with a specific constructor function
>>> function Hero(){}
>>> var h = new Hero();
>>> var o = {};
>>> h instanceof Hero;
true
>>> h instanceof Object;
false
>>> o instanceof Object;
true
Functions that Return Objects
function factory(name) {
return {
name : name
};
}
>>> var o = factory('one');
>>> o.name
"one"
>>> o.constructor
Object()
can also use constructor functions and return objects, different from this.
>>> function C2() {this.a = 1; return {b: 2};}
>>> var c2 = new C2();
>>> typeof c2.a
undefined
>>> c2.b
2
Instead of returning the object this, which contains the property a, the constructor returned another object that contains the property b.
Passing Objects
When you copy an object or pass it to a function, you only pass a reference to that object
Consequently, if you make a change to the reference, you are actually modifying the original object
>>> var original = {howmany: 1};
>>> var copy = original;
>>> copy.howmany
1
>>> copy.howmany = 100;
100
>>> original.howmany
100
The same thing applies when passing objects to functions
>>> var original = {howmany: 100};
>>> var nullify = function(o) {o.howmany = 0;}
>>> nullify(original);
>>> original.howmany
0
Comparing Objects
you'll get true only if you compare two references to the same object.
>>> var fido = {breed: 'dog'};
>>> var benji = {breed: 'dog'};
>>> benji === fido
false
>>> benji == fido
false
>>> var mydog = benji;
>>> mydog === benji
true
Built-in Objects
The built-in objects can be divided into three groups
Data wrapper objects—Object, Array, Function, Boolean, Number, and String.
Utility objects—These are Math, Date, RegExp and can come in very handy
Error objects—The generic Error object as well as other, more specific objects
Object
Object is the parent of all JavaScript objects
>>> var o = {};
>>> var o = new Object();
An empty object already contains some methods and properties.
o.constructor
o.toString()
o.valueOf()
No comments:
Post a Comment