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);