JS package or object type of the best programs



The advantages of object-oriented is one of the powerful ability to create their own special class or object, package a group of properties and behavior. Set aside for the performance, JS than object-oriented language such as JAVA and more useful to be flexible, the assembly is very flexible and convenient data structure. So how are we going to use object-oriented thinking to define the class or object JavaScript does?



Problems

If you want to abstract out of a person, then a simple attribute: name, sex, birthday, etc., and methods for: sayHi, the wording is the most basic

var oPerson = new Object;
oPerson.name = "zs";
oPerson.sex = 'boy';
oPerson.birthday = '2001-02-03';
oPerson.sayHi = function()
{
   alert("Hi ! I am "+this.name);
}

Then we have to create a number of people, how should we do? Have to write a number of Object, it may哭死, then how should we do? Some see the following solution:



The following final solution may not be the best, but I am the current level is that they find the best and welcome advice

functon createPerson(name,sex,birthday)
{
  var oPerson = new Object;
  oPerson.name =name;
  oPerson.sex = sex;
  oPerson.birthday = birthday;
 
 oPerson.sayHi = function()
 {
    alert("Hi ! I am "+this.name);
 }
  return oPerson;
}

//那么我们多创建几个人的话,就可以
var person1 = new createPerson('zs','boy','2001-02-03');
var person2 = new createPerson('ls','boy','2001-02-04');
person1.sayHi();
person2.sayHi();

It appears that the seemed to be resolved, but the problem is that you create a number of people to create a few sayHi acts, but they are a feature, how should we do? JS created a flexible solution following the method of the attributes of an object:

function sayHi()
{
 alert("Hi ! I am "+this.name);
}

functon createPerson(name,sex,birthday,fn)
{
  var oPerson = new Object;
  oPerson.name =name;
  oPerson.sex = sex;
  oPerson.birthday = birthday;
  oPerson.sayHi = sayHi;//这里是个函数引用
  return oPersn;
}
var person1 = new createPerson('zs','boy','2001-02-03');
var person2 = new createPerson('ls','boy','2001-02-04');
person1.sayHi(); //outputs "Hi ! I am zs"
person2.sayHi(); //outputs "Hi ! I am ls"

Problem seems solved, but the prototype you see more people will find out a solution, all the function only to create one, and each object has its own instance of the object attributes, see the following code:

Final

functon CreatePerson(name,sex,birthday,fn)
{
  this.name =name;
  this.sex = sex;
  this.birthday = birthday;
}
CreatePerson.prototype.sayHi = function ()
{
  alert("Hi ! I am "+this.name);
}

var person1 = new CreatePerson('zs','boy','2001-02-03');
var person2 = new CreatePerson('ls','boy','2001-02-04');
person1.sayHi(); //outputs "Hi ! I am zs"
person2.sayHi(); //outputs "Hi ! I am ls"

一般情况下,一个对象或者类不只一个方法,需要多个方法配合使用,那么
CreatePerson.prototype={
  sayHi:function()
  {
    alert("Hi ! I am "+this.name);
  },
  walk:function()
  {
    alert("walk,walk");
  },
  ……
}
是不是优雅的多?

Wrote a half yesterday, to see the javascript comrades back on the high-level programming, I have to say yes right, is from above, I do not talk nonsense, and the middle saved a lot of step-by-step guide, such as: factory pattern , constructor methods, the prototype approach, the structure of mixed methods / prototype approach, dynamic prototype approach, mixed-plant model. They want to learn JS comrades back to look at these, will understand what a lot of "why."



Oh, Behind the back is actually very simple.