On object-oriented object in JavaScript

Source: Internet
Author: User
Tags hasownproperty

first, the object1.1 Creating Objects

JavaScript is an object-based (object-based) language, and everything you encounter is almost always an object.
A simple object to create:

var People = {    "eavan",    +,    getname:function ( {        alert (this. name);        // Eavan     }}

You can use People.name to get the name property of the People object, or People.getname () to get the people name value.
Another way to create an object:

var New  "eavan"= function () {    alert (  this. name);}    

New is used here, and by the way, what happens when you use new, in fact, when you use new, you can roughly think of doing these three things, and look at the following code:

var People  = {};                      // we have created an empty object people people.__proto__ = Object.prototype;   // we point the __proto__ member of this empty object to the object prototype member object Object.call (people);         // We replace the this pointer of the object function with people, and then call the object function
1.2 Package

In simple terms, the hidden fields of some properties are exposed, such as private properties, private methods, common attributes, common methods, protection methods, and so on. JS can also implement attributes such as private properties, private methods, common attributes, common methods, and so on.

Object-oriented programming languages like Java typically have a concept of a class that enables encapsulation. JavaScript does not have the concept of class, JS in the implementation of encapsulation is mainly by function.

First, declare a function to be stored inside a variable. Then, inside the function (class), add a zodiac or method to the class by adding a property or method to the this variable.

varperson =function () {varName ="Eavan";//Private Propertiesfunction CheckName () {};//Private Methods     This. MyName ="gaof";//Object Common Properties     This. myfriends = ["AA","BB","cc"];  This. copy = function () {}//Object Common Methods     This. GetName = function () {//Constructor Method        returnname;            }; }

The problem with pure constructors encapsulating data is that the creation of a method such as This.copy = function () {} is not necessarily bound to a particular object at execution time, it is the same as a global variable, and its process is equivalent to instantiating a function, You don't have to instantiate so many of the same things. The solution to this small problem can be solved with a prototype model.

1.3 Understanding Prototypes

Each time a function is created, a prototype property is generated, which points to the prototype object of the function. It is used to include properties and methods that are shared by all instances of a particular type. Therefore, the instances and methods that are added directly to the prototype are shared by all instances.

Also, as an example of the person above, we can add new properties and methods to their prototypes.

true;                          // static Common properties of a class (objects cannot be accessed) Person.prototype.sex = "man";            // Common properties of a class Person.prototype.frends = ["Gao", "Li", "du"function() {};    // common methods for classes

The problem with prototype encapsulation data: A variable that is bound to a reference type on a prototype, because it is shared by all objects, where one object modifies the data, and when other objects access the data, the value that is accessed is modified.
For example, the following code:

var New Person ();p Erson1.frends.push ("DD"); Console.log (person1.frends);     // ["Gao", "Li", "du", "DD"] var New Person ();p Erson2.frends.push ("ee"); Console.log (person2.frends);      // ["Gao", "Li", "du", "dd", "EE"]

Originally want to Person1 and person2 The Friends attribute add new content separately, the result of the Friends attribute of both is actually "public"!

In general, the most common approach should be to combine constructors and prototype patterns, constructors to define instance properties, and prototype patterns to define methods and shared properties.

Each class has three parts: the first part is within the constructor, which is used for instance object replication. The second part is the constructor, which is added directly through the point syntax for the class to use, and the instantiated object cannot be accessed. The third part is the prototype of the class, where the instantiated object can be accessed indirectly through its prototype chain, and is shared for all instantiated objects.

When it comes to the properties of an object instance, we have a question of whether the property belongs to an instance or to the prototype of the instance when it accesses a property.

For example, in the example above, we add a sex attribute to the Person2 instance, and when we access the sex property of Person2, we get the added value. When adding a property to an object instance, this property masks the property of the same name saved in the prototype object.

   Person2.sex = "Woman";    Console.log (person1.sex);                 // Mans    Console.log (person2.sex);                // woman

At this point we can use the hasOwnProperty () method to detect whether a property exists in an instance or in a prototype. If this attribute is in the instance, hasOwnProperty () returns True, and hasOwnProperty () does not perceive the properties in the prototype. So you can use this method to detect whether a property exists in an instance or in a prototype.

Console.log (Person1.hasownproperty ("Sex"));        // The properties in the prototype return FalseConsole.log (person2.hasownproperty ("Sex"));        // property in the instance, returns True

So much for the next update.

On object-oriented object in JavaScript

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.