<! DOCTYPE html>
<title> Browser Objects </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<body>
<script>
<!--Normal mode of creating objects--
var person = new Object ();
Person.name = "Name1";
Person.job = "Job1";
Person.getname = function () {
return this.name;
}
document.write ("Normal mode:" + person.getname () + ' <br> ');
<!--Summary: If you create objects using the normal pattern above, once you have created more objects, you will create code redundancy, such as 40 students in a class must have a new 40 object-->
<!--Create Object Factory mode--
function CreateObject (name,job) {
var o = new Object ();
O.name = name;
O.job = job;
O.getname = function () {
Return this.name
};
return o; Remember to return the object o
}
var person = CreateObject ("FactoryName1", "manage");
var Anotherperson = CreateObject ("FactoryName2", "manage");
document.write ("Factory mode:" + person.getname () + ', ' + anotherperson.getname () + ' <br> ');
<!--Summary: You can find the Factory mode creation object as long as the parameters can be passed, the factory will have a special machine to make products (objects) can be likened to the ordinary model of the factory assembly line workers, And the factory model can be likened to the machine inside the factory. The following Factory mode is improved by customizing the constructor mode--
<!--Create custom constructor patterns for objects--
function Person (name,job) {
THIS.name = name;
This.job = job;
This.getname = function () {
return this.name;
};
}
var person1 = new Person (' milk pull ', ' assist ');
var person2 = new Person (' symplectic ', ' carry ');
document.write ("Custom constructor mode:" + person1.getname () + ', ' + person2.getname () + ' <br> ');
<!--Summary: Note that this mode and Factory mode is still different, 1. Function name the first letter of P is uppercase this is the constructor of the reference Java,c, 2, the constructor does not have a return value, 3, the function is called with the new keyword; 4, Any function that is used by the New keyword is a constructor--
</script>
</body>
JavaScript Learning notes-creating object Design Patterns