JavaScript Definitive Guide 6th edition-[Read Notes]

Source: Internet
Author: User
Tags object serialization tojson

JavaScript Basics Page (1) <script> properties: async,charset,defer= ' defer ', language obsolete, src,type (2) NULL is a null value, is a special object;  Undefined represents a null value, it is not an object is undefined meaning; Undefined==null returns true; (3) Global variables: Global properties: Undefined, Infinty, Nan Global functions: IsNaN () [Not a number is not a numeric return true], parseint (), Eval () constructor: Date (), REGEXP (), String (), Object () Global object: Manth, JSON (4) The original value in JavaScript:Undefined,null, Boolean, number, string and object(Arrays and functions) are not the same, are immutable; (5) Delete o.x can delete a property of an object, (6) Compound statement: use {} to enclose several statements to form a compound statement; empty statement:; directly with a semicolon; for example: for (var i=0; i&lt ; a.length; a[i++]=0); (7) With: For temporary expansion of the scope chain; Disadvantages:very difficult to optimize and run more slowly;          For example: with (object); Statement   Adds the statement object to the head of the scope chain, then executes statement finally restores the scope chain to its original state;  (8) "Use strict" is an instruction introduced by Ecmasript 5, It can only appear at the beginning of the code or at the beginning of the function body;        in a script or function, subsequent code will parse into strict code, strict code execution; strict mode in ECMAScript 5 is a restricted subset of the language , it corrects important flaws in the language, and provides robust error-checking and enhanced security mechanisms, which differ from non-stone bridge modes: 1) Strict mode is forbidden to use with; 2) all changes are declared in strict mode, and if not declared, the use will lead to a fault, (non-stone bridge mode, An implicitly declared global variable adds a property to the global object) 3) The This value in a function called in strict mode is undefined (not strictly the This value is always a global object)   4) in strict mode, defining two or more attributes with the same name in an object's direct volume will result in a syntax error, not strictly the following;  5) there are two or more parameters in the function declaration that have the same name in strict mode will produce a syntax error (not strictly)   (9) object is mutable, to manipulate by reference;:create,set,query,delete,test,enumerate        Each object has three related attributes in addition to attributes:        Object prototype prototype:   pointing to another object, the property of this object inherits from its prototype object;        Object class: is a character that identifies an object type         extended markup for objects Extensible flag: Indicates whether a new property can be added to the object   (10) The direct amount of the object: var a = {x:1,y:2};        Object Direct amount is an expression, each operation creates and initializes a new object, and if the object's direct amount is used in a loop inside a repeating function, it creates many new pairsLike;  (11) static function: the Object.create () method creates a new object that acts to prevent accidental modification of an object's properties. p134        Create a new object with prototype inheritance function inherit (p) {    if (p==null) throw TypeError ();  & nbsp if (object.create)    //if p is an object but cannot be null        return object.create (p);    //if Object.create () exists      var t = typeof p;    if (t!== ' Object ' && t!== ' F Unction ') throw TypeError ();    function f () {};    //defines an empty constructor     F.prototype = p;   &NBSP;//Set the prototype property to p    return new F ();    //uses f () to create the inheritance of P}  (6.2) property of the set and query associative array objects: array elements are indexed by string instead of numerically indexed arrays are associative arrays, or called hashes, mappings or dictionaries.JS objects are associative arrays;Inheritance: JS only in the query properties will be aware of the existence of inheritance, setting properties and inheritance is irrelevant; querying a property that does not exist does not cause an error, if the property is not found in its own property or inherited property x o.x returns undefined; If you query an object that is not stored, you will get an error; (6.3) Delete attribute Delete to delete an attribute usage:Delete Book.subtitleNote: Delete Simply disconnects the property from the host object and does not manipulate properties in the property; Delete can only delete its own property and cannot delete the inherited property; To delete an inherited property must remove it from the prototype object that defines the property [affects all objects that inherit from the prototype];           If delete Deletes a property that does not exist, it also returns true; Delete cannot delete properties that can be configured with false properties, such as declaring variables and function declarations to create properties of global objects, such as: delete Object.prototype return False V AR x=1; Delete this.x cannot be deleted; function f () {}; Delete this.f; You cannot delete a global function, (6.4) An object that detects a property JavaScript can be seen as a collection of attributes, a property that checks whether an attribute belongs to an object: in: ' X ' in O returns true as the property of O, False is not, or can be o.x!== undefined Judging is not its property;hasOwnProperty (): Checks whether the given name is an object's own property and returns False if it is an inherited property;
propertyisenumerable (): Returns true only if the property is detected to be property-owned and enumerable, and usually JS-created properties are enumerable; (6.5) enumeration properties are typically iterated using for/in: The built-in method for object inheritance is not enumerable except for the two methods that provide one or two enumerated object properties outside of For/in [ECMAScript 5]:Object.keys (o)Returns an array that consists of the names of enumerable objects in the object;object.getownpropertynames (o)Returns the name of all its own properties, not just enumerable properties; (6.6) Getter and setter define property accessors, note: accessor attributes are var o={data_prop:value that can be inherited; Get Accessor_prop () {/* Function body */}, set Accessor_prop () {/* Function body */}}} (6.7) attribute: Its values: value, writable: Writalbe, enumerable enumberable, configurable : Configurable throughobject.getownpropertydiscriptor ({x:1}, "X")You can get a property descriptor for an object-specific property, and return directly to an inherited property and a nonexistent property: Undefined the attribute that gets the inherited property needs to traverse the prototype chain:object.getpropertyof ()Set the attributes of a property, or let a new property have some attribute:Object.defineproperty (O, "x", {value:1,writable:true,enumerable:false,configurable:true})The result attribute exists but cannot be enumerated;Note: This method can modify an existing attribute value and a newly established property value, but cannot modify the inherited property;Modify multiple properties at the same time:object.defineproperty (o, map table {x:{value:1,writable:true},y{enumerable:false,configurable:true},z:{}) (6.8) Each object has a prototype (prototype) associated with it, class classes, and extensibility The prototype of the Extensibleattribute object is used to inherit the property, and the prototype property is set at the beginning of the instance object creation;An object created by the object's direct volume, using Object.prototype as its prototype;The object created by the new expression uses the prototype of the constructor as its prototype;objects created by object.create () use the first parameter as its prototype (or null);To detect whether an object is a prototype of another object (or in a prototype chain), use the isPrototypeOf () method For example: you mightp.isprototypeof (o)To detect if P is a prototype of O; var p={x:1};  //Define a prototype object var o=object.create (p);      //Use this prototype to create an object p.isprototypeof (o);    //true O inherits from PObject.prototype.isPrototypeOf (p);      //True p inherits from the class property of the prototype  object (class attribute) is a string representing the information of the object type, and neither ECMAS3 nor 5 provides a way to set this property. But there is an indirect way to find it: P151Object.prototype.toString (). Call (obj). Slice (8)   The extensibility of the object is used to indicate whether a new property can be added to an object. All built-in objects and custom objects are explicitly extensible, and the extensibility of the host objects is defined by the JS engine, unless they are converted to non-extensible; ECMAS5 can be used to check whether an object is extensible by using object.esextensible (O) ; Object.preventextensions (o); The object is passed in as a parameter to make the object non-extensible; If you add a property to a prototype of an object that is not extensible, the non-extensible property inherits these new attributes, and the purpose of extensibility is to lock the object from the outside; Object.seal ( ); In addition to setting the object to be non-extensible, you can also set the object's own property to be non-configurable, that is, the object cannot be added to the new property, and the existing properties can not be deleted or configured, but its existing writable properties are still set, for those already closed objects are not solvable, you can use Object.issealed (o); To check if the object is closed;  object.freeze (o) Locks the object more tightly (freezes): In addition to setting the object to be non-extensible and setting its properties to be non-configurable, all its own data properties can be set to read-only if the object has an accessor setter Method The accessor is not affected; use Object.isfrozen (o) to determine whether an object freezes;  (6.9) object serialization: Refers to the object's state to a string, or the string can be restored to an object ECMAS5 provides a json.stringify () and  json.Parse () to serialize and restore JS objects; These two methods can accept the second optional parameter and customize the custom serialization or restore operation by passing in a list of properties to serialize or restore;  json syntax is a set of words for JS, supports objects, arrays, strings, infinity numbers, True,false,null, and it can serialize and restore the;   (6.10) Object methodtoString ()Method has no parameters: when the array is converted to a string, each element is transformed into a string, and when the function is converted into a character, the source code of the function is obtained;toloalestring ()Returns the localized string for this object P154ToJson ()Object.prototype actually does not have a ToJSON () method, and if an object is to be serialized it can use Json.stringify (obj) This method calls the ToJSON () methodvalueOf ()JS converts an object to a primitive value instead of a string. Seventh Chapter ArrayAn array is an ordered set of values, the JS array is untyped, the array elements can be of any type, and different elements in the same array can be of different types; Maximum index: 2^32-2     Maximum capacity: 4294967295 accessing an array element with an array index is generally faster than accessing regular object properties. Array Inheritance Array.prototype attribute 7.1 Create array: var a=[];    var b=[1,2,3]; var c=[a,1,1.1, "a",]; The last value of C is undefined;     var a=[,] A.length = 2 has only two elements; The constructor creates an array: var a= new Array ();    var b=new Array (10); var c=new Array (5,3,2,1, "string"); 7.2 read/write a[0]; arrays are special situations for objects: using square brackets to access an array's elements is like using square brackets to access an object's properties, JS converts the specified numeric index value to a string of 1 to "1" and then uses it as a property name, and you can use negative and non-integer to index the array; a[-2.13   ] = true; A[1.0]=10 and a[1] are equal a[the value of "]=a[1000", the array index does not have an out-of-bounds concept: a[100] will not error, but will return undefined 7.3 sparse array is an array containing a discontinuous index starting from 0 if the array is sparse, then lengt H value is greater than the number of elements sparse array is usually slower than dense, memory consumption is more, such an array to find the element time and the object's property lookup time is as long; 7.5 adding elements to an arrayA.push ("AB", "CD")Add two elements to the end of the array, or more;A.unshift ("a")Adds an element to the head of the array;Delete A[1]Deleting an array element does not alter the array's. Length property, and the index is not automatically filled, and it becomes a sparse array;pop ()Method andpush ()Used together, reducing the length by 1 and returning the deleted array value;shift ()Method andunshift ()Used together to remove an element from the head of an array; Shif () moves the index of all current elements down to a value 1 lower than the current index;Splice ()A generic method inserts a delete or replace array; it modifies the length property as needed and moves the element to a higher or lower index; 7.6 array traversal var a = Object.keys (o); Gets an array of O object property names for loop traversal; Arr.foreach (function (x) {operation x element value}); 7.8Array.join ()Method var a=[1,2,3] is the inverse operation of String.Split ();A.join ()"A.join" ("") output "123" A.join ("") output "1 2 3"Array.reverse ()Reverses the order of the elements in the array; it does not rearrange the creation of new arrays but instead rearranges them on the original array;Array.Sort ()The original array is sorted and the array is returned, the array is sorted with the alphabet without parameters, and if there is a undefined, it will be at the end of the line;Array.Sort (function (A, b) {compare size})You can pass a method in order to compare the value size;Array.concat ()The Concat method creates and returns a new array whose elements include the Elements and Concat () of the original array that calls the () and each parameterArray.slice (Starindex,endindex)Returns a fragment or sub-array of the specified array; All elements that contain the position of the first parameter but not the position of the second argument; it does not modify the called array; a negative number indicates that the last index-1-3 is the third-lowest index;Array.splice (Para1,para2) The ability to delete an element from an array, insert an element into an array, or complete both operations. It modifies the array of calls, increasing or decreasing the index value of the array as needed; PARA1: Specifies the starting position of the insertion or deletion, PARA2 specifies the number of elements that should be removed from the array, and if the second parameter is saved from the start point to the end, it returns an array of deleted elements ; Paran specifies that any n parameter is an element that needs to be inserted into the array, starting at the position of the first parameter;push () vs. Pop ()Allow arrays to be used as stacks: Psuh () adds one or more elements at the end of the array and returns the new length of the Data, POPs () deletes the last value of the array, and decreases the length of the array, returning the deleted value;Note that both of these methods will modify the original array and not create a new array, and be able to use the array to implement the stack operation after travel;unshift () and Shift ()In contrast to the above two, it is inserted and deleted in the head of the array;toString () and toloalstring ()The array is transformed into a string; 7.9 Ecmas 5 Array Method: Provides 9 new array methods to traverse, map, filter, detect, simplify, search array;function (array element, index, array itself) {action}; pass as 第2-4个 parameterForEach (function (value) {value action})Iterate through the array, cannot use break to stop the loop, but can use Try,cache to stop; var b = a.map (function (x) {Retun x*x})Each element of the call array is passed to the specified function and an array is returned, which functions the return value of the function, and its return value is that the new array does not modify the original array; it will not pass through the sparse array, it will return a sparse array; var a =b.filer (function (x) {x>3})The array element returned by the method is a subset of the call array, and the passed function is used for logical judgment; Returns an array of all the values of the current array >3; it skips the missing elements in the sparse array and always returns a dense array, so it can compress the empty array: var B =A.filer (function () {return ture});You can also delete undefined and null elements return x!==undefined && x! ==null every () and some () Is the logical decision of the array: they use the specified element for the logical determination of the array to return TRUE or false; two methods one but to confirm what value is returned, immediately stop iterating the arrays; var a=[1,2,3]; A.every (function (x) {return x<10}) returns Trueall values below 10 will return true.;for allThe value of the decision returns true until the last return true otherwise returns Faslevar a=[1,2,3,11]; A.some (function (x) {return x>10}) returns Trueonly one item in it will return true if the value is greater than 10All value comparisons return False when they are returned to flase; reduce ()with Reducerigth ()The method uses the specified function to merge the elements of the array, generating a single value, also known as "injected" and "collapsed"; var a=[1,2,3,4,5]; var sum = a.reduce (function (x, y) {return x+y;},0) array sum, (function (x, y) {return x*y},1) array quadrature, (function (x, y) {return (x>y ) (x:y}) maximum value;0,1 The second parameter is actually the initial value;An empty array cannot call reduce () otherwise it will report an exception error; Reduceright () is the same as the reduce principle except that indexes are from high to low, working from right to left, for example, the exponentiation of an array is right-to-left; indexOf () and lastIndexOf ()Searches for an element with the given value in the trimmed array, returns the index of the first element found if not found returns 1 7.10 The type of the array determines whether the arrays are Array.isarray () to determine ECMAS5; [] instanceof array returns true, but it is not a Reliable Array detection method 7.11 Class Array object: Detection method P174 I understand that an array of classes is a JSON string; 7.12 is a string of arrays: String a= ' ABCD '; CharAt (1) return B a[1] return bNote: Using an array of methods to modify the string will cause an error, but not a false hint; Eighth Chapter functionFormal Parameters:The variables passed in the function,actual parameters:The parameters of the run-time function call, the context of this call the JS function is an object, you can assign a function to a variable or as a parameter to other functions, functions can be nested with each other, so that they can access their definition of the use of any variable in the domain, which constitutes a closure The code that makes up the body is not executed directly when it is defined, but only when the function is called;There are four ways to call a function: As a function, as a method, as a constructor, indirectly through their call () and the Apply () method, to determine whether the current corner is a strict mode: var static = (function () {return!this;} ());method Chain:When the return value of a method is an object, the object can also call other methods, the method invocation sequence (often called a hyphen or cascade) each time the result of the invocation is part of another expression, "it is better to return this directly when the method does not need to return a value." In this programming style, you can specify an object that is called one time, such as: Shap.setx. Sety (. setSize). Draw (); ThisThere is no scope limit and cannot be assigned, not a variable or a property; p183 This is used in detail; if a function or method call is preceded by a new keyword, it constitutes a constructor call; 8.3 parameter and argument a=a| | []; Equivalent to if (a===undefined) {a=[]};argument object arguments:The function f argument has only one X, two arguments can be invoked with X or arguments[0] to invoke the first, can be called with arguments[1] to invoke the second argument, and this can only be obtained; The argument object also defines the callee and caller properties in the non-strict mode CA Llee Specifies the function to be executed, caller is non-standard, it specifies the function of the currently executing function, the call stack is accessible through the caller property, and in some anonymous functions callee can recursively call itself P188 can use an object property as an argument: function (ARG) { ARG.A,ARG.B,ARG.C} The function function that calls 8.4 as a value can be assigned to a variable: var a = square; square is a function; You can also assign a property to an object: Var o={add:function (x, y) {return X+y;}}; You can also assign a value directly to an array element: Var a[function (x) {return x*x},20] function can have custom properties: A.count = 0;function A () {return a.count++;} ; Call a property value +1; 8.5 functions as a command space P194 8.6 closure P196 8.7 Function Properties, methods, and constructors: a function can also have properties and the length property of a method function "such as: A.length" is a read-only representation of the number of arguments to the function [A parameter is a reference to the number of arguments that are given when the function is defined]function A (ARG) {var a = Arg.length;    var B = arg.callee.length;    if (a!== b) {throw Error ("Two parameters not the same as Doha");    }};function f (x, Y, z) {a (arguments); return x + y + z;} F (1,2,3,4) contrasts with F (prototype) property: Each function includes a prototype attribute, which is a reference to an object called the prototype object; (prototype object);Each function contains a different prototype object, and when the function is used as a constructor, creating a new object inherits the property from the prototype object;Checks whether an object is a true function: (and has a function method) to detect its Classn property: function () isfunction (x) {return Objet.prototype.toString.call (x) = = = [ Object function] '} 8.8 Functional Programming: You can use the array's map () and reduce () to achieve the calculation of the average and standard deviation of the array; var sum = function (x, y) {return x+y;};  Data=[1,2,3,4,5,6,7,8,9];var aaa=data.reduce (sum)/data.length; AAA as average;Higher order functions:An action function that takes one or more functions as a parameter, returning a new function; P209 Nineth Chapter classes and modulesThe implementation of the class is based on the prototype inheritance mechanism; Class 9.1 and prototype: all instance objects of a class inherit properties from the same prototype object; The prototype object is the core of the class, and the 9.2 class and constructor: P216 defines the class, and the first letter of the class name is capitalized, and the first letter of the normal function and method is lowercase Definition class: Function Run () {} class (constructor call must be called with new keyword) prototype object naming: Run.prototype={fun1:function () {},fun2:functioni () {},...}this call to the run () constructor automatically uses Run.prototype as the prototype for the new run object;9.3 Class inheritance in JavaScriptConstructor Object: The name of the class is defined for the JS object, and any attribute added to the constructor object is the field and method of the class, and if the property value is a function, it is the class method;Prototype Object: The properties of the prototype object are inherited by all instances of the class, and if the property of the prototype is a function, the function is invoked as a method of the instance of the class;Instance Object: Each instance of the class is a separate object, the attributes defined directly to the instance are not shared for all instance objects, and the non-function attributes defined on the instance are the fields of the instance;JS defining class Steps: P2201, defining a constructor, and setting the properties of the initialization new object, 2, the method of defining an instance for the prototype object of the constructor, 3, defining the class field and class property for the constructor, and the instance method of the class defined as the function Value property of the prototype object;Note: The instance method of JS must use the This keyword, and the class's methods usually do not use the This keyword, they refer to its parameter operation;The P222-based inheritance mechanism in the extended p223js of class 9.4 is dynamic: Objects inherit properties from other prototypes, and if the properties of the prototype change after the object is created, it also affects all instance objects that inherit the prototype, and we can augment the JS class by adding a new method to the prototype object 9.5 The left side of the class and type instanceof operation is the object to be detected for other classes, and the right operand is the constructor that defines the class: If O inherits from C.prototype, the expression o instanceof C returns to Ture where inheritance is cumulative, and actually detects the inheritance of the object. Instead of detecting the constructor that creates the object, and the other way to identify whether the object belongs to a class is to use the constructor property; Tenth-Regular Expressions10.1 Regular Expression definition: You can use the REGEXP () constructor to create a RegExp object, but more is created by five special direct volume syntax, contained in a pair of slashes/between: Var parttern =/s$/matches all strings ending with s---- Follow up with time to fill

JavaScript Definitive Guide 6th edition-[Read Notes]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.