Object-oriented is a method of understanding and abstracting the real world, and is a product of computer programming technology development to a certain stage.
The meaning of the object
The object can be a car, a person, an animal, a text, a form, or anything that exists, etc.
Objects are:
Attributes -----------------------------------------------------------------------------------------------------------------------------
Method -------------------------------------------------------------------------------------------------------------------------------
Events -------- can respond to what happens to the object.
We can understand object-oriented by creating a person's object
people:
Two hands, two feet, one head, and you can run.
Hands, feet, and head are human attributes, and running is human method.
First, let's create an object in the easiest way
var person = { head: "one", hand: "two", foot: "two", run : function(){ console.log("running"); } }This method is not practical at all because it is to create a separate object, and this object has no connection with any common data structures.
Then, we create an object using the constructor
var Person = function(){//Note that the initial letter is capitalized this.head = "one", this.hand = "two", this.foot = "two", this.run = function(){ alert("running"); } } var Joan = new Person(); document.write(Joan.run())// "running"This is an object created with a constructor, and then we add a line of code to see
var Niki = new Person(); alert(Joan==Niki) //false;
Yes, two different object instances are now created.
Each function in JavaScript has a prototype attribute. If a function is used as a constructor, this attribute will be automatically created by a new call to create the prototype of the object.
console.log(Joan)
You can see that there is a __proto__:Person, where __proto__ is the prototype chain of Joan. It is the prototype pointing to Person.
When JS creates an object (whether it is a normal object or a function object), it has a built-in property called __proto__, which is used to point to the prototype object prototype of the function object that created it.
Some understanding of prototype chains is written in very detailed in the book JavaScript Advanced Programming. If you are interested, you can check it out. There are also PDF documents available online. But it is recommended to buy this book, which supports the original version.
Then any changes to the prototype property of prototype can be applied to each instance object constructed with new Person(), whether it is created before or after the change. Add a new function to Person.prototype. The details are as follows:
var Person = function(){//Note, the initial letter is capitalized this.head = "one", this.hand = "two", this.foot = "two" } Person.prototype.run = function(){ alert("running"); } var Joan = new Person(); Joan.run()// "running" alert(Joan.__proto__===Person.prototype)//'true'As you can see, the creation method in the prototype can be called, and Joan's prototype chain points to Person's prototype.
Look again:
var Niki = new Person();//"runing" Person.prototype.run = function(){ alert("running running") } Joan.run()//"running running" Niki.run()//"running running"See, modifying Person's prototype method, all methods in object instances created by new Person() are modified because all instances share the same prototype method run. This is an application of prototypes.
This is some understanding of creating objects.
I've written for a long time. I don't know if there is any error. If there is any error, please give me some advice.
Next time I will write about object-oriented inheritance.
The above is all the contents of the comprehensive understanding of Js OOP programming creation objects brought to you by the editor. I hope everyone will support Wulin.com more~