Home>

method one:Created by the built-in object and then through the dot syntax,Create properties for objects dynamically,method

var o1=new object ();
o1.name="tom";
o1.sing=function () {
 console.log ("i am singing");
}

Way two:Create objects by literal json

var o2={
 "name":"jackson", "age":"10", "say":function () {
  console.log ("say hi");
 };
};

Way three:Created by the constructor

var o3=function () {
 this.name="lucy", this.age="10", this.say=function () {
  console.log ("say hello");
 }
}

Way four:Created from a prototype

var o4=function () {}
o4.prototype={
 "name":"taylor", "age":10, "say":function () {
  console.log ("say h1");
 }
}

Way five:Created by a mixture of prototype and constructor (recommended,Why:Try to define methods as prototype methods,Prototype methods avoid the construction of properties or methods each time a constructor is called,save space,Create objects fast)

var o5=function () {
 this.name="james";
 this.age=10;
}
o5.prototype={
 "say":function () {
   console.log ("say hi");
 }
}

Way six:Create objects in copy mode

//first need a copy module
function extend (target, source) {
 for (var k in source) {
  target [k]=source [k];
 }
 return target;
};
var o6={
 "name":"o6", "age":10
}
var o7={
 "say":function () {
  console.log ("say hi");
 }
}
//o6 copies o7's method and builds it into a new object
var o8=extend (o6, o7);
o8.say ();//say hi

Way seven:Create objects from third-party libraries

There are base2.js and simplejavascriptinheritance.js to achieve, there are materials on the Internet.

  • Previous Detailed tutorial on online deployment after Vue + Koa2 is packaged
  • Next Method for using VMware virtual machine to access Internet using NAT mode