A common way to create custom types is to use the constructor pattern in combination with the prototype pattern.
The constructor pattern is used to define the specific parts (properties and methods) of the instance object, and the prototype pattern is used to define the shared part.
This saves the maximum memory overhead.
function Human (name, sex) { this.name = name;
this.sex = sex; this.getwife=function () {//Marry wife if (this.sex = = "Male") { return "
I want to marry a Wife "; } Else return "I don't have to marry a wife"; } } Human.prototype.say = function () { alert (
THIS.name); } var man = new Human ("Lv bu", "male")
;
var woman = new Human ("Diao cicada", "female"); alert (Man.getwife ());//I want to marry my wife Alert (Woman.getwife ());//I don't have to marry my wife alert (Man.getwife = = Woman.getwife);//false alert (man.say==woman.say);//true
This combination pattern has the advantage of the function construction pattern and the prototype model. Solved their own shortcomings. This combination mode is the most widely used and most popular way to create a custom type.