


A preliminary understanding of javascript object-oriented_javascript skills
Foreword
Class-based objects: We all know that an obvious sign in object-oriented languages is the concept of classes. Through classes, which are similar to templates, we can create many objects with the same properties and methods. However, there is no concept of classes in ECMAScript, so naturally it will be different from objects in class-based languages.
Objects in js: An unordered collection of attributes, which can include basic values, objects, and functions. That is, an object in js is a set of values in no specific order. Each property or method of the object has its own name, and each name corresponds to a value.
Understanding object
How to create objects
1 The simplest way to create an object is to create an Object instance and then add properties and methods to it.
For example
var person = new Object(); person.name='谦龙'; person.sex='男'; person.sayNameAndSex=function(){ console.log(this.name,this.sex) } person.sayNameAndSex(); // 谦龙 男
2 Use object literal form
For example
var person={ name:'谦龙', sex:'男', sayNameAndSex:function(){ console.log(this.name,this.sex) } } person.sayNameAndSex(); // 谦龙 男
Type of attribute
ECMAScript has two types of data attributes: data attributes and accessor attributes.
Data attributes
The data attribute contains the location of a data value. Values can be read and written at this location. There are four properties that describe its behavior.
1.[[Configurable]]: Indicates whether the attribute can be redefined by deleting it through delete...The default value is true
2.[[Enumerable]]: Indicates whether the attribute can be returned through for in loop...The default is true
3.[[Writable]]: Indicates whether the value of the attribute can be modified...The default is true
4.[[Value]]: Indicates the value of this attribute. The default is undefined
To modify the default properties of a property, you must use the ES5 Object.defineProperty() method, which receives three parameters: the object where the property is located, the name of the property, and an object describing the property properties (configurable, enumerable , writable, value), setting one or more of them can modify the corresponding characteristics
DEMO
var person={}; Object.defineProperty(person,'name',{ configurable:false,//表示不允许通过delete删除属性 writable:false,//表示不允许重写 ennumerable:false,//表示不允许通过for in遍历 value:'谦龙'//设置该对象中属性的值 }) person.name='谦龙2';//尝试重新设置 结果不生效 delete person.name;//尝试删除 结果不生效 for(var attr in person){ console.log(person[attr]);// false } console.log(person.name);//谦龙
Note: After setting configurable to false, it is not allowed to be changed to true again. In addition, when calling the Object.defineProperty() method, the default values of configurable, ennumerable, and writable are false.
Accessor properties
Accessor properties do not contain data values. They contain a pair of getter and setter functions (but these two functions are not necessary). When reading the accessor properties, the getter function will be called. This function is responsible for returning a valid When writing the accessor property, the setter function is called and the new value is passed in. This function is responsible for how to process the data.
Accessor properties have the following characteristics
[[configurable]] indicates whether attributes can be deleted through delete to define new attributes
[[enumerable]] indicates whether the returned attributes can be traversed through a for in loop
[[get]] Function called when reading properties, the default is undefined
[[set]] The function called when writing the function. The default value is undefined
Note: Accessor properties cannot be defined directly and must be defined through Object.defineProterty()
DEMO
var book={ _year:2015, //这里的下划线是常见的记号,表示只能通过对象的方法才能访问的属性 edition:1 } Object.defineProperty(book,'year',{ get:function(){ return this._year; //即默认通过 book.year获取值的时候 返回的是 boot._year的值 }, set: function (value) {//在对 boot.year设置值的时候 默认调用的方法 对数据进行处理 var _year=this._year; if(value > _year){ this._year=value; this.edition+=value-_year; } } }) book.year = 2016; console.log(book.year,book.edition); // 2016 2
Define multiple attributes
We can add multiple properties to an object through the Object.defineProperties() method in ES5. This method accepts two object parameters. The first parameter is the object whose properties are to be added and modified, and the second parameter is The attributes correspond to the attributes to be added and modified in the first object.
DEMO
var book={}; Object.defineProperties(book,{ _year:{ value:2015, writable:true //注意这里设置成true 才可以 "写" 默认是false }, edition:{ value:1, writable:true //注意这里设置成true 才可以 "写" 默认是false }, year:{ get:function(){ return this._year; }, set: function (value) { var _year=this._year; if(value > _year){ this._year=value; this.edition+=value-_year; } } } }) book.year=2016; console.log(book.year,book.edition); // 2016 2
Read properties of object properties
Using the Object.getOwnPropertyDescriptor() method in ES5, you can get the descriptor of a given property.
This method receives two parameters: the object where the attribute is located and the attribute name of the descriptor to be read. What is returned is an object. If it is a data attribute, the returned attributes are configurable, enumerable, writable, value. If it is an accessor attribute, the returned attributes are configurable, enumerable, get, set
DEMO
var book={}; Object.defineProperties(book,{ _year:{ value:2015, writable:true }, edition:{ value:1, writable:true }, year:{ get:function(){ return this._year; }, set: function (value) { var _year=this._year; if(value > _year){ this._year=value; this.edition+=value-_year; } } } }) //对象遍历函数 function showAllProperties(obj){ for(var attr in obj){ console.log(attr+':'+obj[attr]); } } var descriptor= Object.getOwnPropertyDescriptor(book,'_year');//数据属性 var descriptor2= Object.getOwnPropertyDescriptor(book,'year');//访问器属性 showAllProperties(descriptor); console.log('============================'); showAllProperties(descriptor2);
That’s all the above introduction to the preliminary understanding of object-oriented JavaScript Pay attention.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes
