开胃菜, var a = [2,3,1,3,8,4,3]; 如何快速找到8的索引位置?
开始的一句话: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference 这个星球最好的js文档之一
ie9+ (2012年才基本普及,跟现在的es6差不多)
in操作符的使用, 查找属性是否存在当前对象(包括原型也算)
var a = {b:‘bbb‘,c:‘ccc‘}; function b(){ this.d=‘ddd‘ }; b.prototype=a; var bb = new b(); ‘d‘ in bb;
Boolean
x = Boolean(expression); // 这样用
x = new Boolean(expression); // 而不要这样!
!! //才是王道
Number
Number(thing)
new Number(thing)
String
String(thing)
new String(thing)
String.fromCharCode(65); //A unicode ---> 65
Date
RegExp
Error
Function
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()来定义", 这个是错误的,我爱李松峰的翻译,这个事肯定是曹力干的.