Home>

Object-oriented is a way to understand and abstract the real world.It is the product of the development of computer programming technology to a certain stage.

The meaning of the object

The object can be a car,People, animals, text, forms or anything that exists.

The objects are:

Properties ------- some specific properties of the object.

Methods ------- Objects can do.

Events ------- can respond to things that happen to the object.

We can understand object-oriented by creating a human object

people:

Hold two hands, two feet, and one head, and you can run.

The hands, feet, and head are human attributes,Running is a human way.

First, let's create an object in the simplest way

var person={
      head:"one",      hand:"two",      foot:"two",      run:function () {
        console.log ("running");
      }
    }

This method is not practical at all,Because it is creating a separate object,And this object has nothing to do with any common data structure.

Then we create an object using the constructor

var person=function () {//Note, the first 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 the object created with the constructor,Then we add another line of code to see

var niki=new person ();
    alert (joan == niki) //false;

Yes, two different object instances are now created.

Every function in javascript has a prototype property. If a function is used as a constructor,Then this property will be automatically created by the new object prototype

console.log (joan)

You can see that there is a __proto __:person, where __proto__ is the prototype chain of joan.It is a 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__.The prototype object used to point to the function object that created it.

Some understanding about the prototype chain,It is written in detail in the book Advanced JavaScript Programming.Those who are interested can check it out.There are also pdf documents available online.But it is recommended to buy a book,Support the original version.

Then any changes to the prototype property of prototype can be applied to every instance object constructed with new person (),It doesn't matter whether it was created before or after the change.Add new function for person.prototype.details as follows:

var person=function () {//Note, the first 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"

can be seen,Creating methods in the prototype is callable,At the same time, Joan's prototype chain points to the prototype of person.

Look again:

var niki=new person ();//"runing"
     person.prototype.run=function () {
       alert ("running running")
     }
     joan.run () //"running running"
     niki.run () //"running running"

Look, modify the prototype method of person,All methods in the object instance created by new person () have been modified,Because all instances share the same prototype method run. This is an application of prototypes.

That's some understanding about creating objects.

Wrote for a long time.I don't know if there are any errors. If there are errors,You are welcome to give pointers.

Next time I will write about object-oriented inheritance.

  • Previous Detailed usage examples of spl_autoload_register () function in PHP
  • Next ASPNET Core 10 implements mail sending function