Home  >  Article  >  Web Front-end  >  A brief discussion on object-oriented programming in JavaScript

A brief discussion on object-oriented programming in JavaScript

PHPz
PHPzOriginal
2016-05-16 16:18:501262browse

This article mainly shows you the relevant information on JavaScript object-oriented programming. Friends in need can refer to it.

ECMA-262 defines an object as: "A collection of unordered attributes, whose attributes can include basic values, objects or functions"

The easiest way to understand objects is to create an Object instance, and then add attributes and methods to it

var person = new Object();
person.name = "Xulei";
person.age = "23";
person.job = "前端工程师";
person.sayName = function () {
    alert(this.name);
}

You can also write like this

var person = {
     name: "xulei",
     age: 23,
     job: "前端工程",
     sayName: function () {
          alert(this.name)
     }
}

Attribute type: data attribute and access its attribute

1. Data attributes, there are 4 characteristics that describe their behavior

[Configurable]: Indicates whether the attribute can be redefined by deleting it through delete, and whether it can be modified The characteristics of the attribute, or whether the attribute can be modified as an accessor attribute, the default value is true;
[Enumerable]: indicates whether the attribute can be returned through for-in, the default value is true;
[Writable]: indicates whether the attribute can be returned through for-in, the default value is true; Whether the attribute can be modified, the default value is true;
[Value]: contains the data value of this attribute. The default value is undefined.

var person = {
      name: "xulei"
}

A person object is created here, and the value is "xulei"

To modify the default characteristics of the attribute, you must use ECMAScript5's Object.defineProperty (the object where the attribute is located, the attribute's Name, descriptor object)
The descriptor object must be configurable, enumerable, writable, value

var peron = {}
        Object.defineProperty(peron, "name", {
            writable: false,//属性不能被修改
            value: "徐磊-xulei"
        });

        alert(peron.name);//徐磊-xulei
        peron.name = "徐磊";
        alert(peron.name);//徐磊-xulei

The above operations will be ignored in non-strict mode assignment operations, and will be thrown in strict mode Exception

Once a property is defined as non-configurable, it cannot be changed back to configurable.

In most cases it is not necessary to take advantage of these advanced features provided by the Object.defineProperty() method. But it is very useful for understanding javascript.

Readers are advised not to use this method on ie8.

2. Access its attributes, there are 4 characteristics

[Configurable]: Indicates whether the attribute can be deleted by delete to redefine the attribute, whether the characteristics of the attribute can be modified, or whether the attribute can be changed Modified to accessor attribute, the default value is true;
[Enumerable]: Indicates whether the attribute can be returned through for-in, the default value is true;
[Get]: Function called when reading;
[Set]: Function called when writing properties.

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn