這篇文章是看js紅寶書第8章,記的關于對象的筆記(第二篇)。
創建對象的幾種模式:
工廠模式:
工廠是函數的意思。工廠模式核心是定義一個返回全新對象的函數。
1
2
3
4
5
6
7
|
function getObj(name, age) { let obj = {} obj.name = name obj.age = age return obj } let person1 = getObj( "cc" , 31) |
缺點:不知道新創建的對象是什么類型
構造函數模式:
通過一個構造函數,得到一個對象實例。
構造函數和工廠模式區別是:
1,構造函數函數體加了this
2,構造函數沒有return
3,構造函數調用時,用new關鍵字
1
2
3
4
5
6
7
8
|
function CreateObj(name, age) { this .name = name this .age = age } let person1 = new CreateObj( "cc" , 31) console.log(person1) console.log(person1.constructor === CreateObj); // true console.log(person1 instanceof CreateObj); // true |
關于構造函數的兩個問題:
1,構造函數和普通函數唯一區別是調用方式。構造函數要用new關鍵字。如果不用new,則是往Global對象上加屬性。下面例子中,CreateObj方法,往window對象上加了name和age屬性
1
2
3
4
5
6
|
function CreateObj(name, age) { this .name = name this .age = age } CreateObj( 'cc' , 10) console.log(window.name) // 'cc' |
2,構造函數存在的問題: 構造函數體內的方法,每次創建一個實例,都會創建一遍。
1
|
person1.sayName( ) === person2.sayName( ) // false |
解決方法是,將sayName定義在createObj外面。
1
2
3
4
5
6
7
8
9
10
|
function sayName() { console.log( this .name) } function CreatePerson(name, age) { this .name = name this .age = age this .sayName = sayName } let person1 = new CreatePerson( 'joy' , 31) person1.sayName() |
但是,這樣會讓自定義類型引用的代碼不能很好聚在一起。
原型模式:
原理是,每個函數都有一個prototype屬性。prototype是一個對象,里面定義的屬性和方法會被所有實例共享。
關于原型模式,有兩個等式:
1
2
3
4
|
function Person() { } let person1 = new Person() console.log(person1.__proto__ === Person.prototype) // true console.log(Person.prototype.constructor === Person); // true |
關于原型對象的三個方法:isPrototype , getPrototypeof,setPrototypeOf, Object.create()
1
2
3
4
|
// isPrototypeOf判斷構造函數的原型對象是否是實例的原型對象 function Person() {} let person1 = new Person() console.log(Person.prototype.isPrototypeOf(person1)); // true |
1
2
3
4
|
// 獲取對象的原型對象 function Person() {} let person1 = new Person() console.log(Object.getPrototypeOf(person1) === Person.prototype); |
1
2
3
4
5
|
// 將某個對象,設為另一個對象的原型對象 let person1 = {name: "cc" } let person2 = {age: 32} Object.setPrototypeOf(person1,person2) console.log(person1.name, person1.age); // cc 32 |
1
2
3
4
5
|
// 以某個對象為原型對象,創建一個新對象 let person1 = {name: "cc" } let person2 = Object.create(person1) person2.age = 30 console.log(person2.name, person2.age); |
當訪問一個對象person的name屬性時,是按照以下步驟去訪問:
1,person上如果有name屬性(即便這個屬性是null,也會返回null),返回name屬性值;沒有,繼續去原型對象Person.prototype上找
2,如果原型上有name屬性,返回原型上name屬性值;沒有,則返回undefined
判斷一個屬性是在實例上,還是在原型上,可以用hasOwnProperty
1
2
3
4
5
|
function Person() {} Person.prototype.name = "cc" let person1 = new Person() console.log(person1.name) // 'cc' console.log(person1.hasOwnProperty( "name" )); // false |
判斷一個對象上是否有個某個屬性,用in操作符
1
2
3
4
5
6
|
// 對象自身 or 原型上找到,都返回true function Person() {} Person.prototype.name = "cc" let person1 = new Person() console.log( "name" in person1) // true console.log(person1.hasOwnProperty( "name" )); // false |
訪問對象的屬性的方法:
1
2
3
4
5
|
Object.keys( ) for ... in // 繼承屬性也會遍歷出來 Object.getOwnPropertyNames(obj) // 會列出可枚舉和不可枚舉屬性,其他和 Object.keys( )結果一樣 Object.getOwnPropertySymbols(obj) // 和getOwnPropertyNames類似,只是僅針對symbol Reflect.ownKeys(obj) // 和Object.keys( )結果一樣 |
其他訪問對象屬性和屬性值的方法:
Object.values( ) 對象值組成的數組,會省掉Symbol型。
Object.entries( ) 對象鍵值對組成的數組,會將鍵,轉化成字符串,省掉Symbol型。
1
2
3
4
5
6
7
8
|
function Person() {} Person.prototype.name = "cc" let person = new Person() person.age = 21 let sy = Symbol( 'sy' ) person[sy] = 'smile' console.log(Object.values(person)) // [ 21 ] console.log(Object.entries(person)) // [ [ 'age', 21 ] ] |
到此這篇關于詳解js創建對象的幾種方式和對象方法的文章就介紹到這了,更多相關js創建對象內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://segmentfault.com/a/1190000039302326