


Learn javascript object-oriented and understand javascript objects_javascript skills
1. Programming Thoughts
Process-oriented: Centered on the process, gradually refined from top to bottom, the program is regarded as a collection of function calls
Object-oriented: Objects serve as the basic unit of the program, and the program is decomposed into data and related operations
2. Classes and objects
Class: an abstract description of things with the same characteristics and characteristics
Object: a specific thing corresponding to a certain type
3. Three major characteristics of object-oriented
Encapsulation: Hide implementation details and achieve code modularization
Inheritance: extend existing code modules to achieve code reuse
Polymorphism: different implementation methods of interfaces to achieve interface reuse
4. Object definition: A collection of unordered attributes, whose attributes can include basic values, objects or functions
//简单的对象实例 var person = new Object(); person.name = "Nicholas"; person.age = 29; person.job = "Software Engineer"; person.sayName = function(){ alert(this.name); }
5. Internal attribute types: Internal attributes cannot be accessed directly. ECMAScript5 puts them in two square brackets and divides them into data attributes and accessor attributes
[1] The data attribute contains a data value location where the value can be read and written. Data attributes have 4 characteristics:
a. [[Configurable]]: Indicates whether the attribute can be redefined by deleting the attribute through delete, whether the characteristics of the attribute can be modified, or whether the attribute can be modified as an accessor attribute. For attributes defined directly on the object, the default value is true.
b. [[Enumerable]]: Indicates whether the attribute can be returned through a for-in loop. The attribute is defined directly on the object. The default value is true
c, [[Writable]]: Indicates whether the value of the attribute can be modified. For attributes defined directly on the object, the default value is true
d. [[Value]]: Contains the data value of this attribute. When reading the attribute value, read it from this location; when writing the attribute value, save the new value in this location. Properties defined directly on the object, the default value is undefined
[2] The accessor property does not contain a data value , but contains a pair of getter and setter functions (but these two functions are not required). When the accessor property is read, the getter function is called, which is responsible for returning a valid value; when the accessor property is written, the setter function is called and the new value is passed in, and this function is responsible for deciding how to handle the function. Accessor properties have the following 4 characteristics:
a. [[Configurable]]: Indicates whether the attribute can be redefined by deleting the attribute through delete, whether the characteristics of the attribute can be modified, or whether the attribute can be modified into an accessor attribute. Properties defined directly on the object, the default value is true
b. [[Enumerable]]: Indicates whether the attribute can be returned through a for-in loop. The attribute is defined directly on the object. The default value is true
c. [[Get]]: Function called when reading attributes. The default value is undefined
d.[[Set]]: Function called when writing attributes. The default value is undefined
6. Modify internal properties: Use the object.defineProperty() method of ECMAScript5, which receives three parameters: the object where the property is located, the name of the property and a descriptor object
[Note 1]IE8 is the first browser version to implement the Object.defineProperty() method. However, this version of the implementation has many limitations: this method can only be used on DOM objects, and only accessor properties can be created. Due to incomplete implementation, it is not recommended to use the Object.defineProperty() method in IE8
[Note 2]Browsers that do not support the Object.defineProperty() method cannot modify [[Configurable]] and [[Enumerable]]
[1] Modify data attributes
//直接在对象上定义的属性,Configurable、Enumerable、Writable为true var person = { name:'cook' }; Object.defineProperty(person,'name',{ value: 'Nicholas' }); alert(person.name);//'Nicholas' person.name = 'Greg'; alert(person.name);//'Greg'
//不是在对象上定义的属性,Configurable、Enumerable、Writable为false var person = {}; Object.defineProperty(person,'name',{ value: 'Nicholas' }); alert(person.name);//'Nicholas' person.name = 'Greg'; alert(person.name);//'Nicholas'
//该例子中设置writable为false,则属性值无法被修改 var person = {}; Object.defineProperty(person,'name',{ writable: false, value: 'Nicholas' }); alert(person.name);//'Nicholas' person.name = 'Greg'; alert(person.name);//'Nicholas'
//该例子中设置configurable为false,则属性不可配置 var person = {}; Object.defineProperty(person,'name',{ configurable: false, value: 'Nicholas' }); alert(person.name);//'Nichols' delete person.name; alert(person.name);//'Nicholas'
[Note] Once a property is defined as non-configurable, it cannot be changed back to configurable. That means you can call Object.defineProperty() multiple times to modify the same property, but after setting configurable to false , there are restrictions
var person = {}; Object.defineProperty(person,'name',{ configurable: false, value: 'Nicholas' }); //会报错 Object.defineProperty(person,'name',{ configurable: true, value: 'Nicholas' });
[2] Modify accessor properties
//简单的修改访问器属性的例子 var book = { _year: 2004, edition: 1 }; Object.defineProperty(book,'year',{ get: function(){ return this._year; }, set: function(newValue){ if(newValue > 2004){ this._year = newValue; this.edition += newValue - 2004; } } }); book.year = 2005; alert(book.year)//2005 alert(book.edition);//2
[Note 1] Only specifying getter means that the attribute cannot be written
var book = { _year: 2004, edition: 1 }; Object.defineProperty(book,'year',{ get: function(){ return this._year; }, }); book.year = 2005; alert(book.year)//2004
[Note 2] Only specifying setter means that the property cannot be read
var book = { _year: 2004, edition: 1 }; Object.defineProperty(book,'year',{ set: function(newValue){ if(newValue > 2004){ this._year = newValue; this.edition += newValue - 2004; } } }); book.year = 2005; alert(book.year);//undefined
[Supplement] Use two non-standard methods to create accessor properties: __defineGetter__() and __defineSetter__()
var book = { _year: 2004, edition: 1 }; //定义访问器的旧有方法 book.__defineGetter__('year',function(){ return this._year; }); book.__defineSetter__('year',function(newValue){ if(newValue > 2004){ this._year = newValue; this.edition += newValue - 2004; } }); book.year = 2005; alert(book.year);//2005 alert(book.edition);//2
7. Define multiple properties: ECMAScript5 defines an Object.defineProperties() method, which can be used to define multiple properties at once through descriptors. This method receives two object parameters: First The first object is the object whose properties are to be added and modified. The properties of the second object correspond one-to-one with the properties of the first object to be added or modified
var book = {}; Object.defineProperties(book,{ _year: { value: 2004 }, edition: { value: 1 }, year: { get: function(){ return this._year; }, set: function(newValue){ if(newValue > 2004){ this._year = newValue; this.edition += newValue - 2004; } } } });
八、读取属性特性:使用ECMAScript5的Object.getOwnPropertyDescriptor()方法,可以取得给定属性的描述符。该方法接收两个参数:属性所在对象和要读取其描述符的属性名称,返回值是一个对象。
[注意]可以针对任何对象——包括DOM和BOM对象,使用Object.getOwnPropertyDescriptor()方法
var book = {}; Object.defineProperties(book,{ _year: { value: 2004 }, edition: { value: 1 }, year: { get: function(){ return this._year; }, set: function(newValue){ if(newValue > 2004){ this._year = newValue; this.edition += newValue - 2004; } } } }); var descriptor = Object.getOwnPropertyDescriptor(book,'_year'); alert(descriptor.value);//2004 alert(descriptor.configurable);//false alert(typeof descriptor.get);//'undefined' var descriptor = Object.getOwnPropertyDescriptor(book,'year'); alert(descriptor.value);//'undefined' alert(descriptor.configurable);//false alert(typeof descriptor.get);//'function'
以上就是关于javascript面向对象的详细内容介绍,希望对大家的学习有所帮助。

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.