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面向对象的详细内容介绍,希望对大家的学习有所帮助。

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)