There are three types of objects in javascript: 1. Internal objects such as Array, Boolean, Data, Math, Number, Object, RegExp, and String objects. These Object systems provide us with their own properties and methods. call. 2. class-based objects use classes to reference objects. These classes need to be defined by ourselves. 3. Prototype-based objects provide guidance on how to use javascript prototype-based object models, it also provides links to specific information that describe the User-Defined constructors and Inheritance of prototype-based objects. /* Add other attributes or methods to the javascript built-in object */String. prototype. good = function () {// custom method return "APPEND good method";} String. prototype. bad = "APPEND bad attribute"; // custom attribute var str = ""; // defines a String instance str and calls the attributes and methods of String object append document. write (str. good (); document. write (str. bad); custom object syntax rules 1. Object creation method 1). object initialization method format: objectName = {property1: value1, property2: value2 ,..., PropertyN: valueN} property is the property value of an object. The value can be a string, number, or object. Example code: var user = {name: "Sn ", age: 25}; 2) Compile a constructor using the constructor method and create an object using the new method. The constructor can carry the sample code of the constructor parameter: function f (name, age) {this. name = name; this. age = age;} var use = new f (); C # definition: constructor 1. The constructor name must be exactly the same as the class name; 2. the constructor function is mainly used to define the initialization status when the class object is created. It does not return values or can be modified using void. 3. The constructor cannot be called directly, it is automatically called only when an object is created through the new operator. Generally, a method is called when the program executes the object. 4. when defining a class, the constructor of this class is usually displayed, and the initialization work specified in the function can also be omitted; 5. When a class is only If a private constructor is defined, its object cannot be created using the new keyword (inaccessible because it is restricted by the protection level). When a class does not define any constructor, C # The Compiler automatically generates a default no-argument constructor for the constructor. Unless the class is a static class, the javascript definition: constructor In the constructor javascript allows a return value, normally, the return value is not set. If a constructor has a return value, the returned object becomes the value of the new expression. In this case, the object as this will be discarded. Constructor: allows you to copy your own objects multiple times, and the instance can access its internal attributes and methods based on the set access level. After the object is instantiated, the constructor immediately executes any code it contains/* The function definition function is declared in this way: the function keyword, function name, a set of parameters, and the code to be executed in parentheses. There are three types of function construction Syntax: 1. function functionName (arg0, arg1 ,... argN) {statements} // function statement 2.var function_name = new Function (arg1, arg2 ,..., argN, function_body); // Function () constructor 3.var func = function (arg0, arg1 ,... argN) {statements}; // sample code of function direct quantity: */function fn (x) {return x * x}; // 1. function statement var Fnc = new Function ("x", "return x * x;"); // 2. function () constructor var fact = function (x) {return x * x ;}; // 3. function Direct Volume, anonymous function, expression document. write (fn (10); // display 100document. write (Fnc (10); // display 100 document. write (fact (10); // display 100/* Note: If the function does not have a clear return value or the return statement without parameters is called, the true return value is undefined. * // * Function () constructor is actually a fully functional object. The Function class can represent any Function defined by the developer. Syntax for directly creating a Function using the function class: var function_name = new Function (arg1, arg2 ,..., argN, function_body) in the above form, each arg is a parameter, and the last parameter is the function body (the code to be executed ). These parameters must be strings. Sample Code: */var f = new Function ("x", "x * x;"); document. write (f (10); // display: 100document. write (f); // display: function anonymous (x) {return x * x;} anonymous (anonymous) document. write (f (10); // undefineddocument. write (f); // display: function anonymous (x) {x * x;}/* Note: Although Function constructors can be used to create a function, it is best not to use it, it is much slower to define a function than to use a traditional method. However, all functions should be considered as Function class instances. If the defined function does not have a parameter, you only need to pass a string (the main body of the function) to the constructor. Note: none of the parameters passed to the constructor Function () indicates the name of the Function to be created. Untitled functions created by using Function () constructor are sometimes called "anonymous functions ". Function () allows us to dynamically create and compile a function, which does not limit us to the Function body pre-compiled by the function statement. * // * A function directly calls a function. It can define an anonymous function. The syntax of a function's direct quantity is very similar to that of a function statement, except that it is used as an expression rather than a statement and does not need to specify a function name. Syntax: var func = function (arg0, arg1 ,... argN) {statements}; // The number of functions directly creates an unnamed function, but its syntax also specifies that it can specify the function name, this is useful when writing and calling your own recursive functions. Sample Code: */var f = function fact (x) {if (x <= 1) return 1; else return x * fact (x-1);}; document. write (f (10); // display: random number, because the return value is less than 0/* Note: it does not actually create a function named fact, only allow the function body to reference itself with this name. The function name does not have any substantive significance. It is just used to save the variable name of the function. This variable may only be saved in different locations and can be assigned to other variables, it will still work in the same way. * // * Difference between Function () constructor and Function Direct Volume Function () constructor and Function direct volume is one of the Differences: Using Constructor () the created functions do not use lexical scopes. On the contrary, they are always compiled by top-level functions, for example, sample code: */var y = "global"; function constructFunction () {var y = "local"; return new Function ("return y;"); // Function () constructor without local scope} function constFunction () {var y = "local"; var f = function () {// function quantity directly, use the local scope return y ;}; return f ;} document. write (constructFunction (); // display global, because Function () constructor returns a Function that does not use a local scope document. write (constFunction (); // display lobal, because the function directly returns the function and uses a local scope/* Note: it does not actually create a function named fact, only allow the function body to reference itself with this name. The function name does not have any substantive significance. It is just used to save the variable name of the function. This variable may only be saved in different locations and can be assigned to other variables, it will still work in the same way. * // * Constructor in javascript allows the return value, but usually does not set the return value. If a constructor has a return value, the returned object becomes the value of the new expression. In this case, the object as this will be discarded. */Function defines the constructor function f (x) {this. x = x;} document. write (f (10); // display: 10var o = new f (10); document. write (o. x); // display: 10/* javascript object {} // define object syntax var object ={}; // attribute syntax in the object (property) this attribute value is a pair of values.) object. property = value; // function syntax in the object (the function name (func) is paired with the function content) object. func = function (){...;}; sample Code: */var object ={}; object. property = 'value'; object. func = function () {return 'fn '}; document. write (object. property ); // Display: valuedocument. write (object. constructor); // display: function Object () {[native code]} document. write (object. func. constructor); // display: function Function () {[native code]}/* prototype and property prototype are attributes of the constructor, this attribute points to an object. The attributes of the object prototype are found in all instances. // Define object syntax obj. prototype = {}; sample code: */var object = {}; object. func = function () {return 'fn '}; object. func. prototype = {prop: 'attribute name'}; object. func. prototype. newsprop = {prop: 'add new attributes dynamically'}; object. func. prototype. newsfn = function () {return 'and then dynamically Add a new method'}; document. write (new object. func (). prop); // display: attribute value document. write (new object. func (). newsprop. prop); // display: add the new property document dynamically. write (new object. func (). newsfn (); // display: Add a new Method/* Note: The new instance is required. Of course, the prototype and attribute name have no substantive significance like the function name, but they are used to save the variable name of the function, this variable may only be saved in different locations. You can assign this function to other variables and it will still work in the same way. */This. privileges (attributes and methods), prototype. public (attributes and methods), function. (attributes and methods) <script type = "text/javascript"> function f (x) {this. public_v = x; // privileged attribute (Public attribute), which can be called only in the instance after being instantiated, that is, new f (). x var private_v; // Private Attribute. You can only use var new_f = this within the constructor; // this is the currently executed object, that is: new_f = this = new f (); function private_fn () {// Private method, call the attribute value of the instance object, that is, get_x = new f (). x; return new_f.x;} this. publice_fn = function () {// privileged method (public method). return x can be called only in the instance after being instantiated; // in the public Methods can access private members and public attributes and Methods} f. prototype. prototype_f = function () {// public method, which must be instantiated using the new keyword, // adding a member to prototype adds the new method to the bottom layer of the constructor and returns the member added by prototype! ';} F. static_p = 'static attribute'; // static attributes, which cannot be used for new. It is applicable to special instances of objects and is the constructor f of the Function object instance. static_f = function () {// static method, which cannot be used for new return 'static method';} var o = new f (10); // new instantiate document. write (o. public_v + '\ n'); // display: 10document. write (o. publice_fn () + '\ n'); // display: 10document. write (o. prototype_f () + '\ n'); // display: members added by prototype! Document. write (f. static_p); // display: static attribute document. write (f. static_f () + '\ n'); // display: static method/* Note: If no instance exists, this. privileges (attributes and methods), prototype. public (attributes and methods), function. (properties and methods) cannot call (privileged) private or (privileged) public members within the function, each instance created by the constructor contains copies of the same private and public members, as a result, the more instances occupy the more memory */js object-oriented design is better than function () {} (constructor) {} can be regarded as a single instance (Singleton mode) function () {} and return an object. Every time a NEW object is returned, it is a different object! The above also said {} is more efficient. And new is not required. {} Is globally fixed and can be expanded at will. In general, {} is indeed highly efficient and easy to use. However, you do not want to expose some methods. In this case, use function to better parse JSON 1 and JSON (javascript Object Notation) using javascript) is a lightweight data exchange format. JSON is a subset of javascript syntax. In javascript, {} represents an object, and [] represents an array, the called syntax is the same as {} and [] In javascript. JSON is constructed in two structures: 1. ordered list of values, array json ([{"name": "value"}]). 2. There is an unordered list of values ({"name": "value "}). JSON has the following forms: 1. a json object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs. 2. An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas. 3. The value can be a string, number, true, false, null, or object enclosed by double quotation marks (not single quotation marks) or array ). These structures can be nested. 4. A string is a collection of any number of Unicode characters enclosed by double quotation marks. It is escaped using a backslash. A character (character) is a separate string (character string ). JSON file format and JSON file annotation JSON file format. The suffix is. json. JSON itself is used to compress text, so you cannot add comments to the JSON file. Javascript and json. details used together by js 1. JSON writing and JSON converting to {} object JSON writing: {"name": "value"} incorrect writing: {'name ': 'value'} JSON to {} object, with the help of json. js conversion. Correct syntax: var obj = '{"name": "value"}'; incorrect syntax: var obj = "{" name ":" value "}";}"; sample Code json for object {} And json conversion. js: <script src = "/js/jquery. js "type =" text/javascript "> </script> <script src ="/js/json. the js "type =" text/javascript "> </script> example uses JSON. parse converts the string into a json object <script type = "text/javascript"> var jsonText = '{"name": "sn", "age": "25 "}'; // Note: Each attribute name must be enclosed in double quotation marks. Var jsonObj = JSON. parse (str); // convert the string to the json object document. write (jsonObj. name); // display: sn </script> the sample uses JSON. stringify converts a json object into a string <script type = "text/javascript"> var o = {name: 'sn ', age: 25}; var jsonText = JSON. stringify (o); // converts a json object into a string document. write (jsonText); // display: '{"name": "sn", "age": "25"}' </script>
Document: Click to download