JavaScript objects: A collection of unordered properties, each of which can contain basic values, objects, or functions. Example:
1 var person = new Object (); 2 person.name = "Nicholas"; 3 person.age = 29;4 person.job = "Software Enginee R "; 5 person.sayname = function () {6 alert (this.name); 7 };8 9 person.sayname ();
The person is an object, and name, age, job, Sayname are all properties of the object. Where Sayname is a special property, it has a function.
In fact, a function is also a type, a reference type, and an object is a reference type, so you can re-describe the JavaScript object: The object has properties, and each property can contain the base type and the reference type.
- Object is created:
- Create an object instance, and then add the properties:
1 var person = new Object (); 2 person.name = "Nicholas"; 3 person.age = 29;4 person.job = "Software Enginee R "; 5 person.sayname = function () {6 alert (this.name); 7 };8 9 person.sayname ();
- Direct use of literal:
var person={ Name: "JAY", age:29, job: ' singer ', sayname:function () { alert (this.name);} };
- policies for creating objects
- Factory mode (encapsulates the process of creating objects to make the code neater):
function Createperson (name, age, Job) { var o = new Object (); O.name = name; O.age = age; O.job = job; O.sayname = function () {alert (this.name); }; return o; } var = createperson ("Nicholas", Person1, "Software Engineer"); var person2 = Createperson ("Greg", "Doctor"); Disadvantage: The object is created, but if you ask what type of Person1, Person2, is not recognized. Workaround: Constructor mode.
- Constructor mode:
function person (name, age, Job) { this.name = name; This.age = age; This.job = job; This.sayname = function () { alert (this.name); }; } var person1 = new Person ("Nicholas", "Software Engineer"); var person2 = new Person ("Greg", "Doctor");
- Prototype mode (instance sharing properties and methods)
Attributes are added when the function is created, and each function has a hidden property: Prototype (prototype).
Prototype is a pointer to an object that contains some properties and methods shared by all instances.
function person () { } Person.prototype.name = "Nicholas"; Person.prototype.age =; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () { alert (this.name); }; var person1 = new Person (); Person1.sayname (); "Nicholas" var person2 = new Person (); Person2.sayname (); "Nicholas" alert (person1.sayname = = person2.sayname); True
Very similar to the parent class. At this point, Person1 and Person2 's sayname are the same reference.
- When you create a new function, a, the prototype property is automatically created for the function. This property points to the prototype object B of the function.
B has a constructor property that contains a pointer to a.
- The prototype relationship is determined by b.isprototypeof (Person1).
- The
- cannot modify the property values of a prototype through an object instance.
function person () {} Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () {alert (this.name); }; var person1 = new Person (); var person2 = new Person (); Person1.name = "Greg"; alert (person1.name); "Greg"? from Instance alert (Person2.name); "Nicholas"? From prototype
- Simpler prototype syntax:
function person () { } person.prototype = { name: "Nicholas", age:29, job: "Software Engineer", sayname:function () { alert (this.name); }};In this way, the constructor property no longer points to person. (because creating a function generates a prototype property that points to the address of the prototype object, the constructor property of the prototype object points to person, and in this way, prototype is overridden, The resulting prototype object B is not an original prototype object, so the constructor of B will no longer point to a.
JavaScript Note 3-Object-oriented programming-creating objects