in操做符的使用, 查找屬性是否存在當前對象(包括原型也算)javascript
var a = {b:'bbb',c:'ccc'}; function b(){ this.d='ddd' }; b.prototype=a; var bb = new b(); 'd' in bb;java
Boolean
x = Boolean(expression); // 這樣用
x = new Boolean(expression); // 而不要這樣!
!! //纔是王道
Numberes6
Number(thing)
new Number(thing)express
String
String(thing)
new String(thing)數組
String.fromCharCode(65); //A unicode ---> 65app
Date函數
RegExp測試
Errorthis
Functionspa
arguments.callee; //找到本身
arguments.callee.caller; //找到誰調用的我(找爸爸)
function a(){
console.log(a.caller);
//console.log(a.caller === arguments.callee.caller); //true
};
function b(){
a();
};
b();
bind(); 相似於apply,call的一個方法
var a = {b:'bbb',c:'ccc'};
function f(){
this.b='xxx';
return this.b+this.c;
}
var fun = f.bind(a); //至關於又建立了一個函數,並無破壞f
fun();
console.log(a);
var a = {b:'bbb',c:'ccc'};
function f(param){
this.b=param;
return this.b+this.c;
}
var fun = f.bind(a); //至關於又建立了一個函數,並無破壞f
console.log( fun('xxx') );
console.log(a);
Array
isArray();//判斷是否是數組類型
Array.isArray({}) // false
Array.isArray([]) // true
indexOf(); 返回索引位置,或-1
[1,2,3].indexOf(2); //1
lastIndexOf();
[1,2,3].lastIndexOf(2); //1
forEach(); //沒有返回值
[1,2,3].forEach(function(value,index,array){
console.log("value="+value+",index="+index+",array="+array);
});
[1,2,3].forEach(function(value,index,array){
if( value === 2 ){
return true;
//return false;
}
console.log("value="+value+",index="+index+",array="+array);
});[]
map(); 會覆蓋原數組嗎??
[1,2,3].map(function(value,index,array){
return value*2;
});
var a = [1,2,3];
a.map(function(value,index,array){
return value*2;
});
console.log(a);
filter(); 會覆蓋原數組嗎?
var a = [1,2,3];
a.filter(function(value,index,array){
return value>2;
});
//console.log(a);
some(); //返回false,或者true,遍歷數組是否知足某種狀況
var a = ['男人','女人','妹子','奶哥'];
a.some(function(value,index,array){
return value === '奶哥' ;
});
every(); //返回false,或者true,遍歷數組是否所有知足某種狀況
var a = ['男人','女人','妹子','奶哥變身'];
a.every(function(value,index,array){
return value !== '奶哥' ;
});
reduce() //循環疊加,從頭至尾
var a = [1,2,3,4];
a.reduce(function(prev,cur,index,array){
return prev+cur;
});
reduceRight() //循環疊加,從尾到頭
var a = [1,2,3,4];
a.reduceRight(function(prev,cur,index,array){
return prev+cur;
});
Object
var a = {b:'bbb',c:'ccc'};
function b(a){
a.b='xxx';
}
function c(a){
delete a.b;
}
b(a);
c(a);
console.log(a.b); //這個故事告訴咱們, js的不穩定性.因此推出來了好東西
!!!!極其重要的!!!!
Configurable: 可否delelte刪除從而從新定義屬性,可否修改屬性特性.(!!修改成訪問器屬性或者數據屬性!!)
Enumerable: 表示可否經過for-in循環返回屬性
Writable: 表示可否修改屬性的值
//設置value的叫數據屬性, 設置getter,setter的叫訪問器屬性
Value: 這個屬性的數據屬性, 從這個位置讀, 寫入這個位置.
Getter/Setter, 這個屬性叫訪問器屬性, 從getter讀, 從setter取
Object.keys(); //返回一個指定對象的全部非繼承,可枚舉屬性名的數組
var a = {b:'bbb',c:'ccc'}; Object.keys(a); //["b", "c"]
Object.getPrototypeOf(); 返回指定對象的原型
var a = {b:'bbb',c:'ccc'}; Object.getPrototypeOf(a); //Object {}
var a = {b:'bbb',c:'ccc'}; function b(){}; b.prototype=a; Object.getPrototypeOf(b); //function Empty() {}
var a = {b:'bbb',c:'ccc'}; function b(){}; b.prototype=a; Object.getPrototypeOf(new b()); //Object {b: "bbb", c: "ccc"}
Object.create(指定的原型,屬性); //使用指定的原型及屬性建立一個新對象
var a = Object.create(null); a; //建立空對象
var a = Object.create({}); a; //建立空對象
var a = {b:'bbb',c:'ccc'};
var children = Object.create(a);
children.__proto__ === a; // true;
children.prototype; === undefined; //true
var cc = new children(); //TypeError: object is not a function
function b(){ this.bb='bb';} var a = Object.create(b); new a.__proto__().bb; //bb
function b(){ this.bb='bb';} b.cc='cc'; var a = Object.create(b); a.cc; //cc
var a = Object.create(Array); console.log(a.isArray([])); a === Array; //false; //也就是說新建了一個全局的Array對象 !!挺好玩的
//!!比較重要的例子!!!
var a = {b:'bbb'}; //本身指定的默認都爲true,writable:true,enumerable:true,configurable:true
var children = Object.create(a,{
x:{value:1,writable:false,enumerable:true,configurable:true},
y:{value:2,writable:false,enumerable:false,configurable:true},
z:{value:3,writable:false,enumerable:false,configurable:false},
r:{
get:function(){
return this._r; //這個必須是指向一個其餘字段,否則就會無限遞歸調用
},
set:function(r){
this._r=r;
}
/*writable:false,反覆測試的結果就是不能設, (只指定get,不指定set,意味着不可寫).*/enumerable:false,configurable:false
}
});
children.r=100;
console.log(children.r);
Object.freeze(); 將指定的對象設置爲不能夠改變 增,刪,改都不行,只能查
var a = {b:'bbb',c:'ccc'};
Object.freeze(a);
delete a.b;
a.c = 'xxx';
a.d = 'ddd';
console.log(a.b); //bbb
console.log(a.c); //ccc
console.log(a.d); //undefined
Object.seal(); //阻止添加新屬性或者刪除現有屬性, 增/刪不行, 能夠改和查
var a = {b:'bbb',c:'ccc'};
Object.seal(a);
delete a.b;
a.c = 'xxx';
a.d = 'ddd';
console.log(a.b); //bbb
console.log(a.c); //xxx
console.log(a.d); //undefined
Object.preventExtensions(); //阻止在一個對象上添加新屬性, 新增不行, 刪除,改,查能夠.
var a = {b:'bbb',c:'ccc'};
Object.preventExtensions(a);
delete a.b;
a.c = 'xxx';
a.d = 'ddd';
console.log(a.b); //undefined
console.log(a.c); //xxx
console.log(a.d); //undefined
Object.isFrozen(); //判斷當前對象是否凍結 -->專門針對Object.freeze()的
var a = {aa:'aa',aaa:'aaa'};
Object.freeze(a);
var b = {bb:'bb',aaa:'bbb'};
Object.seal(b);
var c = {cc:'cc',ccc:'ccc'};
console.log(Object.isFrozen(a)); //true
console.log(Object.isFrozen(b)); //false
console.log(Object.isFrozen(c)); //false
Object.isSealed(); //判斷當前是否爲封閉-->專門針對Object.seal()的
var a = {aa:'aa',aaa:'aaa'};
Object.freeze(a);
var b = {bb:'bb',aaa:'bbb'};
Object.seal(b);
var c = {cc:'cc',ccc:'ccc'};
console.log(Object.isSealed(a)); //true
console.log(Object.isSealed(b)); //true
console.log(Object.isSealed(c)); //false
Object.isExtensible(); 判斷當前對象是否能新增屬性, --->針對Object.preventExtensions()的
var a = {aa:'aa',aaa:'aaa'};
Object.freeze(a);
var b = {bb:'bb',aaa:'bbb'};
Object.seal(b);
var c = {cc:'cc',ccc:'ccc'};
console.log(Object.isExtensible(a)); //false
console.log(Object.isExtensible(b)); //false
console.log(Object.isExtensible(c)); //true
Object.propertyIsEnumerable();//檢查對象的某個屬性是否可枚舉(for in) –> 針對enumerable字段
var a = {b:'bbb'}; //本身指定的默認都爲true,writable:true,enumerable:true,configurable:true
var children = Object.create(a,{
x:{value:1,writable:false,enumerable:true,configurable:true},
y:{value:2,writable:false,enumerable:false,configurable:true},
z:{value:3,writable:false,enumerable:false,configurable:false},
});
console.log( children.propertyIsEnumerable('x') ); //true
console.log( children.propertyIsEnumerable('y') ); //false
console.log( children.propertyIsEnumerable('z') ); //false
Object.getOwnPropertyNames(); 返回一個包含指定對象的全部非繼承屬性名的數組,包含不可枚舉類型
var a = {b:'bbb'}; //本身指定的默認都爲true,writable:true,enumerable:true,configurable:true
var children = Object.create(a,{
x:{value:1,writable:false,enumerable:true,configurable:true},
y:{value:2,writable:false,enumerable:false,configurable:true},
z:{value:3,writable:false,enumerable:false,configurable:false},
r:{
get:function(){
return this._r;
},
set:function(r){
this._r=r;
}
}
});
Object.getOwnPropertyNames(children); //["x", "y", "z", "r"]
Object.defineProperty(); //要修改屬性默認的特性,使用它
var person = { name: '1234' }; //這樣定義Configurable,Enumerable,Writable默認爲true,Value等於指定值
var woman = {};
Object.defineProperty(woman,"name",{
configurable:false, //刪除
enumerable:false, //for in
writable:false, //修改
value:'奶哥'
});
console.log(woman.name); //奶哥
delete woman.name;
console.log(woman.name); //奶哥
for(var i in woman){
console.log(i);
}
woman.name='奶哥變身';
console.log(woman.name); //奶哥
Object.defineProperty(woman,"name",{
configurable:true,
});
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;
console.log(book._year);
console.log(book.edition);
Object.defineProperties(對象, 屬性); //設置多個屬性
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;
}
}
}
});
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');
console.log(descriptor);
console.log(typeof descriptor.get);
console.log(typeof descriptor.set);
console.log("----------------------------------------------");
descriptor = Object.getOwnPropertyDescriptor(book,'year');
console.log(descriptor);
console.log(descriptor.value);
console.log(descriptor.writable);
console.log(typeof descriptor.get);
console.log(typeof descriptor.set);
關於對象的補充:
Object.create(); Object.defineProperty(); Object.defineProperties(); 均可以設置對象屬性的狀態.
<javascript高級程序設計>第三版 141頁中 "訪問器屬性不能直接定義,必須使用Object.defineProperty()來定義", 這個是錯誤的,我愛李鬆峯的翻譯,這個事確定是曹力乾的.