Object
JavaScript is an object in addition to numbers, strings, Booleans, nulls, and undefined.
Category of Objects
built-in objects: arrays, functions, dates, regular expressions ...
host object: Window ... document ... (can also be considered as built-in objects)
Custom Objects,
Creating Objects
object literal way:
var object = {id:1,name: ' xxx ', walk:function () {}};
New Constructor
var object = new Object ();
The custom constructor, whose prototype is Object
function Fun () {
}
//fun.prototype = Function;
alert (typeof fun.prototype);//Object
object.create ();
-------------------------------------------
functionFun () {} alert (fun);//This fun refers to the function object that the fun corresponds toAlert (fun ());//this fun () has to be the return value after calling funAlertNewFun ());//This is the call to the fun constructor to return the object created by the fun constructor functionFun () { age=true;//If you do not add this, it is the global//If you declare the Var age that this is a local variable, because the scope of the function will all Var "advance" This. Age = "Fun"; //var age = 123;} alert (NewFun (). age); alert (age);
---------------------------------------------------------------------------------
prototype
function Fun () {
}
The above code is equivalent to var fun = new Function (); Fun.prototype = new Object (); (prototype is a property of a function object, not a property of the object created by the custom function)
The essence of this sentence is to create a fun function object. Just like the class object that created the class after it was loaded. The function object here is equivalent to the class object.
a prototype is a property of a fun function object. So all XXX objects are derived from the same function object, then the same prototype attribute is shared.
What if there's a problem with using prototypes?
each time an object is created, such as var f1 = new Fun (), var F2 = new Fun (), and the literal in the fun is recalculated (reconstructs the object, because the method in JavaScript is the object).
Methods can be reused, but if there are member variables that can cause resource sharing problems, resource sharing issues can be avoided as long as we do not declare member variables.
The purpose of the prototype: to use the function as a method, only when the method used, improper object. Because the method does not have a member variable, only the object has a member variable.
Properties on the prototype chain are not allowed to be assigned values. , the existence of inheritance can only be realized by querying properties
Fun2.prototype.birth = "abc" ; Alert ( new fun2 (). Birth); var fo = new fun2 (); alert (Fo.birth); Fo.birth = "xxxxx"; // cannot be assigned to the birth property on the prototype chain, var Fo2 = new fun2 (); alert (Fo2.birth); // ABC
---------------------------------------------------------------------------------
Json.stringify (o); To convert an O object to a string type
Json.parse (""); To transfer a string to an object type
---------------------------------------------------------------------------------
(function() { alert ("HI"); } ()); anonymous function, you must wrap a bracket outside.
test (); function Test () { var xxx = function () {// This is not an error in this way. Assignment statement, the function that follows will execute alert ("Hello" function () {// declaration statement alert ("www" ); }(); }
closure
Span style= "Font-family:courier new,courier; font-size:14px; " > principle, each time a JavaScript function is called, a new object is created for it to hold the local variable, and the local variable is saved to the scope chain.
var increment = ( function() { var count = 0; return function () { return + +count; }
understood as the nested function executed here } () ) ; for (var i=0;i<10;i++) { Document.writeln (increment ()); // It is understood that the nested function executes the next line after the declaration. };
7-Day introductory JavaScript, fifth day