Details about how to create objects in JS in multiple ways, and js in multiple ways
1. Create a built-in object
var girl=new Object(); girl.name='hxl'; console.log(typeof girl);
2. Factory mode and parasitic constructor Mode
Function Person (name) {var p = new Object (); // instantiate p internally. name = name; p. say = function () {console. log ('My name is '+ p. name);} return p; // Note: Be sure to return} var girl = Person ('haoxioli'); girl. say ();
3. Create a constructor
Var Product = function (name) {this. name = name; this. buy = function () {console. log ('the brand of my clothes is '+ this. name) ;}} var pro = new Product ('devs'); pro. buy ();
4. Prototype creation. Disadvantages: Each attribute in the instance may be different.
Var Role = function () {} Role. prototype. name = {nickName: 'Nick '}; var boy = new Role (); boy. name. nickName = 'Liu bin'; console. log (boy. name. nickName); // Liu Xiaobing var girl = new Role (); girl. name. nickName = 'hao Xiaoli '; console. log (boy. name. nickName); // hao Xiaoli, because the instance object can modify the value of the referenced object in the prototype console. log (girl. name. nickName); // hao Xiaoli
5. hybrid mode: Prototype + constructor. You can put the attributes that are not modified by the instance into the constructor and modify the attributes in the prototype.
Var Role = function () {this. name = {nickName: 'aaa'};} Role. prototype. age = 30; var boy = new Role (); boy. name. nickName = 'boys'; console. log (boy. name. nickName); // boy var girl = new Role (); girl. name. nickName = 'girer'; console. log (boy. name. nickName); // boy, the instance does not modify the value in the constructor console. log (girl. name. nickName); // girl
6. literal form
Var cat = {name: 'Lucy ', age: 3, sex: 'mother'}; // converts an object to a string console. log (JSON. stringify (cat); // {"name": "lucy", "age": 3, "sex": "Mother"} var dog = '{"name ": "john", "sex": "public"} '; console. log (JSON. parse (dog ). name); // convert a string to an object
7. Copy Mode
Function extend (tar, source) {for (var I in source) {tar [I] = source [I];} return tar;} var boy = {name: 'drunk knight ', age: 20}; var person = extend ({}, boy); console. log (person. name );
8. Third-party framework
// First introduce the package <script src = 'js/base2.js'> </script> // base2 framework, Base. both extend and constructor have fixed usage var Animal = Base. extend ({constructor: function (name) {this. name = name ;}, say: function (meg) {console. log (this. name + ":" + meg) ;}}); new Animal ('lily '). say ('fish ');
Another third-party framework
<Script src = 'js/simplejavascriptinheritance. js'> </script> // simplejavascriptinheritance framework, Class. both extend and init have fixed usage var Person = Class. extend ({init: function (name) {this. name = name ;}}); var p = new Person (); p. name = 'over'; console. log (p. name );
The details of object creation in this JS article are all the content shared by the editor. I hope to give you a reference and support for the help house.