JavaScript Object-oriented summary

Source: Internet
Author: User

Objects (object) should be the most important part of JS, is also very difficult to understand the obscure part of JS. It is also the interview and the frame design of each haunt. In this article, the main reference is JavaScript Red Book (sixth chapter of JavaScript advanced programming) and blogs of major bloggers

Talking about properties of object attributes

After all, it's object-oriented programming, and we'll discuss what properties and attributes the discussion object has before we discuss how to target the object.

Property type

Simply put, an object has four properties:

    • [[Configurable]]: Whether you can delete through Delete, can modify the attributes of the property. Straightforward point: Configurable
    • [[Enumerable]]: enumerated to indicate whether it is possible to return through the for-in loop
    • [[Writable]: writable: Whether the value of the property can be modified
    • [[value]]: Contains the value of the property, which is the corresponding readability. The default values for property types for properties of the above four objects are: true,true,true,undefined.

If you want to modify the property's default attribute, you must pass the Object.defineproperty () method. Roughly as follows:

 var  animal = {};object.defineproperty ( Animal,   " name   " ,{writable:  false    " dog   "  // dog  animal.name =  " cat   " ;console.log (animal.name);  // dog  

A writable:false that represents the value of the non-changing property. As you can see from the above example, when you call the Object.defineproperty () method, the default is False if you do not specify a value for the configurable, enumerable, writable attribute.

Accessor properties

Accessor properties do not contain data values, but include getter and setter functions. The Getter function is called when the accessor property is read, and the function is responsible for returning valid values. When writing accessor properties, return to the setter function and pass in the new value.

    • [[Configurable]]: Indicates whether delete can be deleted via delete. Default is True
    • [[Enumerable]]: As described above Enumerable, the default is True
    • [[Get]]: The method that is called when the data is read. Default is undefined
    • [[Set]]: The method that is called by default when writing properties is worth it. Default is undefined

Here do not do too much explanation, see the example directly (from JS Red Book)

varBook ={_year: -, Edition:1};object.defineproperty (book,' Year',{    Get: Function () {return  This. _year},Set: function (value) {if(value> -){             This. _year =value, This. edition++}} ); Book.year= -; Console.log (book.edition);//2

In fact, for the definition of multiple attributes, we can use the Object.defineproperties method. We can then use the Object.getownpropertydescriptor () method for the properties of the Read attribute.

Creating objects

To create objects, do we not directly implement object creation through the constructor of object or the literal method of objects? Of course, these methods are possible, but there is one obvious drawback: creating many objects with the same interface creates a lot of duplicated code. So here we use some of the following actions

Factory mode

A very basic design pattern, in short, is to use functions to encapsulate the details of creating objects with specific interfaces.

function Createanimal (name,type) {varn \NewObject (); O.name=name; O.type=type; O.sayname=function () {alert ( This. Name)} returno;}varCat = Createanimal (' Kitten','Cat');varDog = Createanimal ('Little Dog','Dog');

JavaScript Object-oriented summary

Related Article

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.