Some comprehensive understanding of JS OOP programming objects-basic knowledge

Source: Internet
Author: User
Tags constructor

Object-Oriented is a method of understanding and abstraction to the real world, which is the product of the development of computer programming technology to a certain stage.

What the object means

Objects can be cars, people, animals, words, forms or anything that exists, and so on.

Objects are:

Properties-------Some specific properties of the object.
Method-------things that an object can do.
Event-------can respond to things that occur on the object.

We can understand object-oriented by creating a person's object

People:

Two hands, two feet, a head, you can run.

The hand, the foot, the head, is the human attribute, the movement is the human method.

First, we'll 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 creates a separate object that has no connection to any of the common data structures.

Then, we create an object in the form of a constructor

var person = function () {//note, initial capital
      this.head = "One",
      This.hand = "Two",
      this.foot = "Two",
      this.run = Fu Nction () {
        alert ("Running");
      }
    var Joan = new Person ();
    document.write (Joan.run ())//"Running"

This is the object created with the constructor, and then we'll add a line of code to see

var Niki = new Person ();
    Alert (Joan==niki)//false;

Yes, it is now created with two different object instances.

Each function in JavaScript has a prototype attribute. If a function is used as a constructor, this property is automatically invoked to create the object's prototype via new

Console.log (Joan)

You can see that there is a __proto__:person, where __proto__ is the prototype chain of Joan. It is a prototype that points 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 points to the prototype object prototype of the function object that created it.

Some of the understanding of the prototype chain is very detailed in the JavaScript Advanced programming book. Interested can go to see, online also have PDF documents can be found. But the proposal is to buy a book, support the original.

Any changes to prototype this stereotype attribute can then be applied to each instance object constructed with new person (), whether it was created before or after the change. Add a new function for Person.prototype. as follows:

var person = function () {//note, initial capital
      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, creating a method in the prototype is callable, while Joan's prototype chain points to the 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 the person's prototype method, all the methods in the object instance created by the new person () are modified, because the same prototype method run is common in all instances. This is an application of the prototype.

This is some understanding of creating objects.

Written for a long time. I don't know if there are any mistakes. If there are mistakes, you are welcome to greatly advise.

Next time you write something about object-oriented inheritance.

The above is small set for everyone to the JS OOP programming to create some of the overall content of the object, I hope that we support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.