isFinity() - 检测是不是无穷值(+infinity,-infinity,NaN返回false);
isNaN() - 检测是否为数值
encodeURI(uri) - 将字符串编码为uri(UniformResourceIdentifier 通用资源标识符)
decodeURI(uri) - 将uri解码
encodeURIComponent() - 将字符串编码成uri组件
decodeURIComponent() - 解码
escape() - 对字符串进行编码
unescape() - 解码
eval() - 把js字符串当作脚本执行
var f1=function(x,y){return x*y;}; 将函数做为参数传递给一个变量
123; 这也是匿名函数
'yolo'; 这也是匿名函数
function f(x,y){
return x()+y();
}
function x(){
return 3;
}
function y(){
return 4;
}
alert(x,y);
alert(f(function(){return 4;},function(){return 5;}));
call()方法调用回调函数 函数名,参数数组
function my(a,b){
return a*b;
}
alert(my.call(my,6,7));
apply()方法调用回调函数 函数名,参数数组app
var params=[3,4];
my.apply(my,params);
(function f1(){
alert('happy day');
})();
(function f1(a,b){
return a+b;
})(a,b);
//1.经过对象字面量的形式建立对象,键名含关键字,特殊字符什么的要用引号括起来
var obj={x:1,y:2,z:3};
//2.经过new建立对象;
var obj=new Object();
var arr=new Array();
var date=new Date();
var reg=new RegExp();//建立正则对象
//3.经过构造函数的形式建立对象
function Test(){}
var obj=new Test();//使用new关键字建立对象,若是直接是函数名,表示调用函数
function Test1(a,b){}
var obj=new Test(1,2);
//4.经过Object.create()建立对象
var obj=Object.create({x:1,y:2});
var obj=Object.create(null);//建立一个没有原型的对象
var obj=Object.create(Object.prototype);//建立一个空对象,prototype是原型的意思
//使用instanceof操做符能够检测一个对象是否由某个指定的构造器函数建立的
function Test1(a,b){}
var obj=new Test1(1,2);
alert(obj instanceof Test1);//检测实例obj在不在Test1构造函数中
//定义对象
var person={
name:'yolo',
age:22,
gender:'female',
addr:'广州'
};
// 调用属性
// alert(person.name);
// alert(person['name']);
// console.log('用户名为:'+person.name);
//添加属性
var obj={};//空对象
obj.name='yolo';
obj['name']='yolo';
//delete删除属性
delete obj.name;
//经过for/in遍历属性
var obj={
name:'yolo',
age:23,
msg:'you here'
}
for(var p in obj){
document.write(p+'\n');//输出的是所有键名
}
//若是对象中有方法,调用时要加括号
var obj={
id:1,
name:'yolo',
age:23,
msg:function(){return 'you here';}
}
document.write(obj.msg()+'<br>');
function foo(){};
foo.prototype.z=3;
var obj=new foo();
obj.x=1;
obj.y=2;
//经过in检测对象上是否有某个属性
console.log('y' in obj);
console.log('x' in obj);
console.log('toString' in obj);
//hasOwnProperty检测自身是否有某个属性,不包含继承的
console.log(obj.hasOwnProperty('x'));
console.log(obj.hasOwnProperty('z'))
关于继承原型的理解图:函数
function foo(){}; foo.prototype.z=3; var obj=new foo(); obj.x=1; console.log('x' in obj); console.log(obj.hasOwnProperty('z')); console.log(obj.propertyIsEnumerable('x'));//属性时本身的而且是可枚举的菜返回true console.log(obj.propertyIsEnumerable('z'));
//返回全部自有属性的名称
console.log() console.log(Object.getOwnPropertyNames(obj)); console.log(Object.keys(obj)); </script>
Object.defineProperty()方法设置属性测试
var obj={};
Object.defineProperty(obj,'name',{
value:'yolo',
writable:true,//设置是否可写
enumerable:true,//是否可枚举
configurable:true //是否可配置(与是否可delete有关)
});
delete obj.name;
console.log(obj.name);
若是属性为不可配置,能够把writeable的true改成false,可是不能够把false改成true
var obj={};
Object.defineProperties(obj,{
'x':{
value:1,
writable:true,
enumerable:true,
configurable:true
},
'y':{
value:2,
writable:true
}
});
console.log(Object.getOwnPropertyDescriptor(obj,'x'));//获取属性特性值
console.log(Object.getOwnPropertyDescriptor(obj,'y'));
var count={
x:1,
y:2,
z:3,
get zhouchang(){return this.x+this.y+this.z},
set fanbei(value){
this.x*=value;
this.y*=value;
this.z*=value;
}
}
console.log(count.zhouchang);//输出6
count.fanbei=2;
console.log(count.zhouchang);//输出12
var obj={};
Object.defineProperty(obj,'x',{
get:function(){return 1;}
});
console.log(obj.x);//输出1
对象的属性this
对象的原型(prototype)指向另外一个对象,本对象的属性继承自它的原型对象
- 经过对象字面量建立的对象使用Object.prototype做为它们的原型,即:var obj={}
- 经过new建立的对象使用构造函数的prototype属性做为原型
- 经过Object.create()建立的对象使用第一个参数(也能够是null)做为原型
- 经过isPrototypeOf()来检测一个对象是不是另外一个对象的原型或者处于原型链中 编码
var obj1={x:1};
var obj2=Object.create(obj1);
var res=obj1.isPrototypeOf(obj2);//检测obj1是不是obj2的原型,结果为true
res=Object.prototype.isPrototypeOf(obj2);//检测Object.prototype是不是 obj2的原型,结果为true
console.log(res);
var obj={};
var res=obj.toString();//输出 "[object Object]"
var arr=[];
res=arr.toString();//教程说是输出 "[object Object]",个人测试输出"",由于不少内置的数组对象会重写了toString()方法
res=Object.prototype.toString.call(arr);//使用回调函数,输出"[object Array]"
console.log(res);
// 也自定义函数获得数据类型
function classof(obj){
if(obj===null){
return NULL;
}
if(obj===undefined){
return 'Undefined';
}
return Object.prototype.toString.call(obj).slice(8,-1);//slice(start,end) 方法可从已有的数组中返回选定的元素。-1指最后一个元素,-2指倒数第二个元素……
//假设返回数组对象"[object Array]",咱们要获得的是Array,因此要从第八个开始
}
var a;
a=function(){};//输出 "Function"
a=window; //输出"global"
res=classof(a);
console.log(res);
//构建对象方法
new Date();默认为当前时间
new Date(timestamp);
new Date(dateString);表示日期的字符串值,要求该字符串能被Date.parse()方法识别
new Date(年,月,日,时,分,秒,毫秒);能够只写部分值
//常见日期函数
var d=new Date();
var res=d.getDate();//返回一个指定的日期对象为一个月中的第几天,返回一个1 到 31的整数值
res=d.getDay();//返回星期中的第几天(0-6)
res=d.getFullYear();
res=d.getMonth();//返回月份(0-11)
res=d.getTime();//返回时间戳
console.log(res);
内建对象之RegExp()对象spa
//方法1
var patt=new RegExp('yolo');
var res=patt.test('my name is yolo');
//方法2
var patt=new RegExp();
patt=/yolo/i;
//方法3
var res=patt.test('my name is Yolo');
//示例
res=/./.test('\n');//.是元字符,返回false
res=/y(?=o)/.test('my name is yolo');//检测y后面紧邻的是o返回真
res=/y(!=o)/.test('my name is yolo');//检测y后面紧邻的不是o则返回真
res=/m/i.exec('my name is Yolo');//输出["m", index: 0, input: "my name is Yolo"]
patt=/i/ig;//加了全局搜索g,从上次查找结束为止开始查找
var str='this is the best time';
var arr;
while((arr=patt.exec(str))!==null){
var msg="找到了"+arr[0]+'!'+'下一个匹配从'+patt.lastIndex+'开始';
console.log(msg);
}
输出:
"找到了i!下一个匹配从3开始"
"找到了i!下一个匹配从6开始"
"找到了i!下一个匹配从19开始"
match匹配,search查找,repalce替换prototype
var str='this is a test';
var res=str.match(/is/i);//输出:["is", index: 2, input: "this is a test"]
res=str.search(/is/i);//输出:2(即返回第一个匹配到的索引位置)
res=str.replace(/is/i,'%');//输出:th% is a test
console.log(res);