Você está na página 1de 4

#1 THE GLOBAL OBJECT(the core Javascript, the globals also exist in client side

javascript):
The global object is a regular JavaScript object that serves a very important pu
rpose: the properties of this object are the globally defined symbols that are a
vailable to a JavaScript program. When the JavaScript interpreter starts (or whe
never a web browser loads a new page), it creates a new global object and gives
it an initial set of properties that define:

global properties like undefined, Infinity, and NaN


global functions like isNaN(), parseInt(), and eval().
constructor functions like Date(), RegExp(), String(), Object(), and Array()
global objects like Math and JSON

Use the JavaScript keyword "this" to refer to the global object from the top-lev
el code(outside any function). {ex: var ex = this;}
In client-side JavaScript, the Window object serves as the global object which h
as a self-referential window property that can be used instead of this to refer,
but it also defines quite
a few other globals that are specific to web browsers and client-side JavaScript
. When first created, the global object defines all of JavaScripts predefined glo
bal values.
But this special object also holds program-defined globals as well. If your code
declares a global variable, that variable is a property of the global object(if
the variable is defined as var... then it can be deleted, if it is defined dire
ctly then it can be deleted as a property of the global object).

#2 OBJECTS
An object is more than a simple stringto-value map. In addition to maintaining i
ts own set of properties, a JavaScript object also inherits the properties of an
other object, known as its prototype. The methods of an object are typically inher
ited properties, and this prototypal inheritance is a key feature of JavaScript. E
ven though strings, numbers, and booleans are not objects, they behave like immu
table objects.
JavaScript objects are dynamicproperties can usually be added and deleted, or can
also be used to simulate static struct/class ...
An object can more or less be called as a set of "properties" with a name-value
pair, addition to which have "property attributes":- "Writable", "Enumerable", "
Configurable".
Objects can be created in 3 ways, lets take a look at two of them,
var x = {harry: "Potter", james: "Lily", "harry potter": "Dark Lord"(if the prop
erty name contains spaces/hyphens... use quotes)};
var y = new Object()/ new Date()/ .......;
Object Prototype: Every object has a sceond associated object that is known as its prototype and t
he main object inherits the properties from the prototype. All objects defined u
sing object literal(new Object() or {}) has the prototype "Object.prototype".
new Array() has Array.prototype and new Date() has Date.prototype.... , Object.p
rototype is the rare object that does not have a prototype(i.e., it does not inh
erit any more properties) while Date.prototype has Object.prototype as its proto

type.... this is called prototype chaining.


The important thing to notice is that each javascript object has two sets of pro
perties, one being its own properties, while the other being the set of properti
es inherited from the prototype.
Object.create(): Object.create() creates a new object, using its first argument as the prototype
of that object and an optional second argument that describes the properties of
the new object.
Object.create({x:1, y:2} or null or Object.prototype), the first case it inherit
s x and y, the second case it inherits nothing(not even basic object methods) an
d in the 3rd case it is a normal empty object.
Very useful as it helps create an heir of the object thus protecting the origina
l object in case you didn t want anything touching your onject.
Querying Object Properties: object.property
object["property"], the second one is a better choice if you want to programatic
ally access different properties as the property is taken as a string, whereas i
n the first case the property is rendered as a identifier.
"in" operator, "hasOwnProperty()" and "propertyIsEnumerable()" methods help you
find out if a property exists in an object or not(only own properties, not inher
ited ones).
To check for a list of all properties(own or inherited, but enumerable) of the o
bject use the for/in loop. ex:- for (l in x){console.log(l);}.
Getter, setter methods have been implemented in the new ECMA script, although su
pport for IE is not known. The function keyword is replaced by get\set keywords.
Ex:var o = { data: p, get acc_data(), set acc_data(l)};, notice how the dat
a and the functions are not seperated by a semicolon.
Delete Property: The delete operator only deletes own properties, not inherited ones. (To delete
an inherited property, you must delete it from the prototype object in which it
is defined. Doing this affects every object that inherits from that prototype.
)
delete o.x; // Delete x, and return true
delete o.x; // Do nothing (x doesn t exist), and return true
delete o.toString; // Do nothing (toString isn t an own property), return true
delete 1; // Nonsense, but evaluates to true
delete Object.prototype; // Can t delete; property is non-configurable
var x = 1; // Declare a global variable
delete this.x; // Can t delete this property
function f() {} // Declare a global function
delete this.f; // Can t delete this property either
this.x = 1; // Create a configurable global property (no var)
delete x; // And delete it
delete x; // SyntaxError in strict mode
delete this.x; // This works
Object Attributes: Prototype Attribute:Refer the Object prototype section above.
Object.getPrototypeOf()(for ECMA Script 5) or o.constructor.prototype(for ECMA
script 3) can be used to get the prototype of an object. isPrototypeOf() perfor
ms a function similar to the instanceof operator

Class Attribute:Information about the type of the object.


function classof(o) {
if (o === null) return "Null";
if (o === undefined) return "Undefined";
return Object.prototype.toString.call(o).slice(8,-1);
}
This classof() function works for any JavaScript value.
Extensible Attribute:Specifies whether new properties can be added to the object or not. ECMAScript
3, all built-in and user-defined objects are implicitly extensible.
Object.isExtensible() -- Specifies if the object can be extended.
Object.preventExtensions() -- Prevent the addition of new props(only to the ob
ject, not to the prototype).
Object.seal() -- Object.preventExtensions() + all of the own properties of tha
t object nonconfigurable.
Object.isSealed() -- Check if the object is sealed or not.
Object.freeze() -- Object.seal() + objects own data properties read-only.
Object.isFrozen() -- Check if it is frozen or not.
Object Serialization: Process of converting an objects state to a string from which it can later be res
tored.JSON.stringify() and JSON.parse().
JSON.stringify() serializes only the enumerable own properties of an object. If
a property value cannot be serialized, that property is simply omitted from the
stringified output.Both JSON.stringify() and JSON.parse() accept optional second
arguments that can be used to customize the serialization and/or restoration pr
ocess by specifying a list of properties to be serialized.
Object Methods:toString(), toJSON(), valueOf(), toLocaleString();

#3 CLASSES AND MODULES


In JavaScript, classes are based on JavaScripts prototype-based inheritance mecha
nism. If two objects inherit properties from the same prototype object, then we
say that they are instances of the same class.
IMP: JavaScripts classes and prototype-based inheritance mechanism are substantia
lly different from the classes and class-based inheritance mechanism of Java and
similar languages. Even though the "classical" class behaviour can be emulated,
it is advisable not to.
Two objects are instances of the same class if and only if they inherit from the
same prototype object.
We have a "instanceOf" operator to check if the object is belonging to the class
we assumed.
Classes and Prototypes: In JavaScript, a class is a set of objects that inherit properties from the same
prototype object. The prototype object, therefore, is the central feature of a
class.
There are multiple ways to deifne a class in javascript, but doing it with the c
onstructor is amongst the finest ways.

1) Create a obj called "methods"(common meths all objs should have) and another
obj called "foo" the main obj you want. Make "foo" instance creator in such a wa
y that it inherits from the "methods" object. Hence, all the objects created usi
ng "foo" will have the methods in "methods". For further info, refer Javascript:
A Defenititve Guide.
2)Create a constructor function "foo", initialize the object specific values usi
ng "this" operator. Now, make the "foo.prototype" have all the common data and f
unctionalities each object is expected to have. Hence, now any object created us
ing the new "foo" will belong to the class of "foo". For further info, refer Jav
ascript: A Defenititve Guide.

Você também pode gostar