學習javascript物件導向 理解javascript物件_javascript技巧
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面向对象的详细内容介绍,希望对大家的学习有所帮助。

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

WebSocket與JavaScript:實現即時監控系統的關鍵技術引言:隨著互聯網技術的快速發展,即時監控系統在各個領域中得到了廣泛的應用。而實現即時監控的關鍵技術之一就是WebSocket與JavaScript的結合使用。本文將介紹WebSocket與JavaScript在即時監控系統中的應用,並給出程式碼範例,詳細解釋其實作原理。一、WebSocket技

將MySQL查詢結果陣列轉換為物件的方法如下:建立一個空物件陣列。循環結果數組並為每一行建立一個新的物件。使用foreach迴圈將每一行的鍵值對賦給新物件的對應屬性。將新物件加入到物件數組中。關閉資料庫連線。

JavaScript和WebSocket:打造高效的即時天氣預報系統引言:如今,天氣預報的準確性對於日常生活以及決策制定具有重要意義。隨著技術的發展,我們可以透過即時獲取天氣數據來提供更準確可靠的天氣預報。在本文中,我們將學習如何使用JavaScript和WebSocket技術,來建立一個高效的即時天氣預報系統。本文將透過具體的程式碼範例來展示實現的過程。 We

JavaScript教學:如何取得HTTP狀態碼,需要具體程式碼範例前言:在Web開發中,經常會涉及到與伺服器進行資料互動的場景。在與伺服器進行通訊時,我們經常需要取得傳回的HTTP狀態碼來判斷操作是否成功,並根據不同的狀態碼來進行對應的處理。本篇文章將教你如何使用JavaScript來取得HTTP狀態碼,並提供一些實用的程式碼範例。使用XMLHttpRequest

PHP中,數組是有序序列,以索引存取元素;物件是具有屬性和方法的實體,透過new關鍵字建立。數組存取透過索引,物件存取通過屬性/方法。數組值傳遞,物件參考傳遞。

在C++中,函數傳回物件需要注意三點:物件的生命週期由呼叫者負責管理,以防止記憶體洩漏。避免懸垂指針,透過動態分配記憶體或返回物件本身來確保物件在函數返回後仍然有效。編譯器可能會最佳化傳回物件的副本生成,以提高效能,但如果物件是值語義傳遞的,則無需副本生成。

PHP中的Request物件是用來處理客戶端傳送到伺服器的HTTP請求的物件。透過Request對象,我們可以取得客戶端的請求訊息,例如請求方法、請求頭資訊、請求參數等,從而實現對請求的處理和回應。在PHP中,可以使用$_REQUEST、$_GET、$_POST等全域變數來取得要求的信息,但是這些變數並不是對象,而是陣列。為了更靈活和方便地處理請求訊息,可

PHP函數可以透過使用return語句後接物件實例來傳回對象,從而將資料封裝到自訂結構中。語法:functionget_object():object{}。這允許創建具有自訂屬性和方法的對象,並以對象的形式處理資料。
