Math
Math.max僻爽、Math.min
var array = [11,23,45,800,22,0,33];
Math.max.apply(Math, array); // 800
Math.min.apply(Math, array); // 0
Math.random
/**
* 隨機(jī)獲取一個(gè)整數(shù)(范圍)
* @param {Number} minValue 小值
* @param {maxValue} maxValue 大值
*/
var getRandomRange = function(minValue, maxValue) {
var degValue = maxValue - minValue + 1;
return Math.floor(Math.random() * degValue + minValue);
}
var colors = ['green','red','blue','black','purple'];
var color = colors[getRandomRange(0, colors.length - 1)]; // 隨機(jī)獲得數(shù)組中的每項(xiàng)
屬性類型
1哮塞、數(shù)據(jù)類型
- configurable 表示能否通過delete刪除該屬性 默認(rèn)值true
- enumerable 表示是否可枚舉 就是 for in 循環(huán)是否可以遍歷到 默認(rèn)值true
- writable 表示是否可以修改 默認(rèn)值 true
- value 屬性的數(shù)據(jù)值 默認(rèn)值 undefined
var person = {};
Object.defineProperty(person, 'name', {
writable: false, // 不可改變
configurable: false, // 不可刪除
enumerable: false, // 不可枚舉
value: 'xiaocheng'
});
console.log(person.name); // xiaocheng
person.name = 'sss';
delete person.name;
console.log(person.name); // xiaocheng
2娃兽、訪問屬性
- configurable 能否通過刪除屬性從而重新定義 默認(rèn)值true
- enumerable 能否枚舉澈蟆, for in 循環(huán)返回 默認(rèn)值true
- get 讀取數(shù)據(jù)時(shí)候調(diào)用 默認(rèn)值 undefined
- set 在寫入屬性時(shí)調(diào)用 默認(rèn)值 undefined
var book = {
_year: 2017,
edition: 1
}
Object.defineProperty(book, 'year', {
get: function () {
return this._year;
},
set: function (newYear) {
if (newYear > this._year) {
this.edition += newYear - this._year;
this._year = newYear;
}
}
});
console.log(book.year); // 2017
book.year = 2019;
console.log(book._year); // 2019
console.log(book.edition); // 3
3叛氨、定義多個(gè)屬性
var book = {};
Object.defineProperties(book, {
_year: {
value: 2017,
writable: true // 不寫無法修改 默認(rèn)為false
},
edition: {
value: 1,
writable: true
},
year: {
get: function () {
return this._year;
},
set: function (newYear) {
if (newYear > this._year) {
this.edition += newYear - this._year;
this._year = newYear;
}
}
}
})
console.log(book); // {_year: 2017, edition: 1}
book.year = 2019;
console.log(book); // {_year: 2019, edition: 3}
讀取屬性
// 承接上面設(shè)置的多屬性
// 數(shù)值屬性
var descriptor = Object.getOwnPropertyDescriptor(book, '_year');
console.log(descriptor); // {value: 2019, writable: true, enumerable: false, configurable: false}
// 對(duì)象屬性
var yearDescriptor = Object.getOwnPropertyDescriptor(book, 'year');
console.log(yearDescriptor); // {enumerable: false, configurable: false}
console.log(yearDescriptor.value); // undefined
console.log(typeof yearDescriptor.set); // function
創(chuàng)建對(duì)象的常用方法
1.工廠模式
function createPerson (name, age, job) {
var o = new Object();
o.name = name || 'xiaocheng';
o.age = age || 24;
o.job = job || 'coding';
o.sayName = function () {
console.log(this.name);
}
return o;
}
var person1 = createPerson('張三', 15, '搬磚');
var person2 = createPerson('李四', 16, '服務(wù)員');
2.構(gòu)造函數(shù)模式
function Person (name, age, job) {
this.name = name || 'xiaocheng';
this.age = age || 24;
this.job = job || 'coding';
this.sayName = function () {
console.log(this.name);
}
}
var person1 = new Person('張三', 15, '搬磚');
var person2 = new Person('李四', 16, '服務(wù)員');
var o = new Object();
Person.call(o, '王五', 16, '廚師');
o.sayName(); // 王五
構(gòu)造函數(shù)模式和工程模式的不同
1、沒有顯示的創(chuàng)建對(duì)象
2蔑祟、直接把屬性和方法賦值給了 this 對(duì)象
3、沒有 return
3.原型鏈模式
function Person () {};
Person.prototype.name = 'xiaocheng';
Person.prototype.age = 24;
Person.prototype.job = 'coding';
Person.prototype.sayName = function () {
console.log(this.name);
}
var person1 = new Person();
特定類型的實(shí)例共享所有屬性和方法
一些判斷方法...
object.hasOwnProperty(name); // 此屬性是否是該對(duì)象的實(shí)例屬性
name in object 判斷屬性是否屬于該對(duì)象 無法區(qū)分是否是實(shí)例屬性還是原型屬性
function hasPrototypeProperty(object, name) {
return !object.hasOwnProperty(name) && (name in object)
} // 判斷是否是原型屬性
獲得可枚舉的實(shí)例屬性 Object.keys(obejct); // 返回值為數(shù)組
function Person () {};
Person.prototype.name = 'xiaocheng';
Person.prototype.age = 24;
Person.prototype.job = 'coding';
Person.prototype.sayName = function () {
console.log(this.name);
}
console.log(Object.keys(Person.prototype));// ["name", "age", "job", "sayName"]
var person = new Person();
console.log(Object.keys(person)); // []
person.name = '張三';
person.age = 16;
console.log(Object.keys(person)); // ["name", "age"]
獲得所有實(shí)例屬性 不管它可不可枚舉 Object.getOwnPropertyNames(object) // 返回值為數(shù)組
function Person () {};
Person.prototype.name = 'xiaocheng';
Person.prototype.age = 24;
Person.prototype.job = 'coding';
Person.prototype.sayName = function () {
console.log(this.name);
}
console.log(Object.getOwnPropertyNames(Person.prototype));// ["constructor", "name", "age", "job", "sayName"]
var person = new Person();
console.log(Object.getOwnPropertyNames(person)); // []
person.name = '張三';
person.age = 16;
console.log(Object.getOwnPropertyNames(person)); // ["name", "age"]
更簡(jiǎn)單的原型方法
function Person () {};
Person.prototype = {
constructor: Person,
name: 'xiaocheng',
age: 24,
job: 'coding',
sayName: function() {
console.log(this.name)
}
}
// 但是這樣做 constructor 變成了可枚舉屬性 我們可以用下面的方法
Person.prototype = {
name: 'xiaocheng',
age: 24,
job: 'coding',
sayName: function() {
console.log(this.name)
}
}
// 如果不設(shè)置 constructor 它的構(gòu)造函數(shù)將會(huì)指向 Object
Object.defineProperty(Person.prototype, 'constructor', {
enumerable:false,
value: Person
});
// 現(xiàn)在 constructor 就變成了不可枚舉的屬性了
- 組合使用構(gòu)造函數(shù)模式和原型模式
function Person (name, age, job) {
this.name = name;
this.age = 24;
this.job = job;
this.friends = ['上帝','耶穌'];
}
Person.prototype = {
constructor: Person,
sayName: function () {
console.log(this.name)
}
};
var person1 = new Person('張三', 15, '搬磚');
var person2 = new Person('李四', 14, '服務(wù)員');
person1.friends.push('如來');
console.log(person1.friends); // ["上帝", "耶穌", "如來"]
console.log(person2.friends); // ["上帝", "耶穌"]
- 動(dòng)態(tài)原型模式
function Person (name, age, job) {
this.name = name;
this.age = age;
this.job = job;
if (typeof this.sayName != 'function') {
// arguments.callee // 返回正在執(zhí)行的函數(shù)對(duì)象 也就是 Person
arguments.callee.prototype.sayName = function () {
console.log(this.name);
}
}
}
var person1 = new Person('xiaocheng', 24, 'coding');
person1.sayName(); // xiaocheng
- 寄生構(gòu)造模式
function Person (name, age, job) {
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function() {
console.log(this.name);
}
return o;
}
var person = new Person('xiaocheng', 24, 'coding');
person.sayName() // xiaocheng
例子 創(chuàng)建一個(gè)特殊數(shù)組
function SpecialArray () {
var array = new Array();
array.push.apply(array, arguments); // 此處 不能寫成 array.push(arguments)
array.toPipedString = function () {
return this.join('-');
};
return array;
}
var colors = new SpecialArray('red', 'block', 'green');
console.log(colors.toPipedString()); // red-block-green