刪除[數(shù)組,對象]的屬性-變量
delete arr[index]
i的y次方
Math.pow(i, y)
去掉數(shù)組的第一個元素
arr.shift()
dom對象
每個標簽都是一個對象
{id:"bbc", title:"title", onClick:function(){..}}
<h1>todo</h1>
{
tagName:"h1",
innerHTML:"todo"
}
undefined有可能像變量一樣
var undefined = 123;
var b = undefined;
alert(b); //output undefined or 123
Number數(shù)據(jù)邊界
# 正無窮大
Number.POSITIVE_INFINITY
# 負無窮大
Number.NEGATIVE_INFINITY
是不是有窮值
isFinite()
是非數(shù)字嗎
isNaN()
換行轉(zhuǎn)義符號,可以寫多行
var str = "bbc\
ddb\
ddb";
基模式[默認基10]
(255).toString(16); //FF
(0xFF).toString(); //255
parseInt
parseInt(number, [2,10,16])
------代碼分割線------
0189 ---> 01
parseInt(12bc) ---> 12
強制true和false的轉(zhuǎn)化
true | false |
---|---|
!null | !" " |
!0 | !true |
!undefined | !(new Object()) |
!"" | !-1 |
所有的對象 |
注:所有的對象轉(zhuǎn)化為boolean時都是true, 包括new Boolean(false), new Boolean();
終止整個循環(huán)
var iNum = 0;
outermost:
for(var i=0; i<10; i++){
for(var j=0; j<10; j++){
if(i == 5 && j == 5){
break outermost;
}
iNum++;
}
}
console.log(iNum++);//55
引用類型
- 創(chuàng)建對象的時候如果沒有參數(shù)可以省略括號
var obj = new Object;
var date = new Date;
- 屬性
- constructor,對象的引用(指針)杏瞻,指向原始的Object()函數(shù)
- prototype,默認返回Object對象的一個實例
- hasOwnProperty(property),判斷對象是否含有某個特定的屬性怎囚,屬性名必須為string類型
- isPrototype(object),判斷該對象是否為另一個對象的原型
- propertyIsEnumerable(property),判斷給定的屬性是否可以用for...in語句進行枚舉
- toString(),返回對象的原始字符串表示得问。
- valueOf(),返回最適合該對象的原始值。大部分與toString()返回值相同媚狰。
- 數(shù)字
var n = 68,
n.toFixed(2); //保留2位小數(shù)
var n = 68.0000000000000000000000000009;
n.toPrecision(3); //只保留3位數(shù)
函數(shù)
(a = Math).sqrt(2);
()作用,將括號中的表達式求值返回給上下文
- arguments
類似數(shù)組渠牲,有l(wèi)ength屬性茉兰,可以用下標訪問arguments[index]
arguments.callee調(diào)用的是函數(shù)本身
function fn(){
alert(arguments.callee);
}
function add(n){
return n>1?n + arguments.callee(n-1):1;
}
- length,就是函數(shù)參數(shù)的個數(shù)
- fn.caller,調(diào)用這個函數(shù)的函數(shù)
閉包
返回給外部一個函數(shù)的引用
繼承
function Demo(){
this.toString = 123;
}
Demo.prototype={//139
name:"CJ",
getName:function(){
return this.name;
}
}/**把一個對象改變?yōu)榱硗庖粋€對象**/
Demo.prototype.name="CJ";/**為對象添加屬性**/146
var d=new Demo();
console.log(d.getName());
注:
- 2種賦值方式意義不一樣8璺辍=矶ぁ!
- 創(chuàng)建對象和添加屬性的沒有順序要求秘案,只要在調(diào)用之間就可以
比如:把139和146換行砰苍,效果依然一樣 - 添加屬性后,所有的實例都會繼承這個屬性[在類上添加]
- 指向另一個對象后阱高,所有的實例并不會繼承這個屬性[在對象上添加]
- 對象之間的繼承不能傳參數(shù)[如果傳赚导,1.只會別執(zhí)行一次2.必須在調(diào)用之前],比如:
function DemoA(name){
this.name = name;
}
function DemoB(){
}
DemoB.prototype = new DemoA("CJ");//只能執(zhí)行一次[就是傳入的值是固定的值]
var d = new DemoB();
console.log(d.name);
console.log(d.name);
# 注:就是不能添加參數(shù)
- 有參數(shù)的繼承是這樣子的[二]
function DemoA(name, age){
this.name = name;
this.age = age;
};
function DemoB(name, age){
this.DemoA = DemoA;
this.DemoA(name, age);
}
var d = new DemoB('CJ', 18);
console.log(d.name);
- 有參數(shù)的繼承還可以是這樣子的[三]
function DemoA(name, age){
this.name = name;
this.age = age;
}
var obj = {};
DemoA.call(obj, 'CJ', 18);//DemoA的控制權(quán)交給obj
DemoA.apply(obj, ['CJ', 18]);//傳遞的參數(shù)是個數(shù)組
console.log(obj.name);
Math.min.apply(null, [2,1,6,8,9]);//借用Math的方法
- 繼承方法
[].join.call(arguments, '\n');
ClassName.isPrototypeof(instantce)
- 繼承已有屬性
function DemoA(){
this.name = "CJ";
}
function DemoB(){
}
子類.prototype = new 父類;//不能傳參數(shù)
Form
form.onreset = function(){} //此方法被廢棄
checkboxRadio[0].checked; //radio,checkbox是否被選中
checkboxRadio[0].defaultChecked; //是否是默認選中
textInput.select(); //選中單選框中的文字
textInput.onSelect() = function(){} //選中事件
select.selectedIndex; //被選中的option的下標
cookie
document.cookie
//name1=value1;name2=value2;name3=value3
typeof document.cookie
//string
// 一次只能賦一個值
document.cookie = "cookieName=cookieValue";
document.cookie = "cookieName2=cookieValue2";
//expires
document.cookie = "cookieName2 = cookieName2;expires=" + d.toGMTString();
- 只能存儲url中的字符類型[用encodeURI, encodeURIComponent編碼]
- cookie是用網(wǎng)站來區(qū)分的
H5本地存儲
可以存儲[數(shù)組,json數(shù)據(jù)赤惊,圖片吼旧,腳本,樣式文件]
localstorage API介紹
//getItem
localStorage.getItem('key1')
//setItem
localStorage.setItem('key1','value1')
//removeItem
//key
localStorage.key(0)
//clear
localStorage.clear()
錯誤處理
try{
}catch(e){
e.message //錯誤信息
}finaly{
}
Ajax
"url?" + Math.random();
問題:前進荐捻,后退會失效
- XMLHttpRequest對象
new XMLHttpRequest(); //>= IE7
new ActiveXObject("Msxml3.XMLHTTP"); //IE6
new ActiveXObject("Msxml2.XMLHTTP"); //IE6
new ActiveXObject("Microsoft.XMLHTTP"); //IE6
- XMLHttpRequest對象參考
onreadystatechange*: 指定當readyState屬性改變時的事件處理句柄黍少。只寫 *表示是W3C文檔對象模型的擴展
//這是一個監(jiān)聽函數(shù)啊
xhr.onreadystatechange = function(){
//your code
}
- readyState: 返回當前請求的狀態(tài)寡夹,只讀
0(未初始化):對象已建立,但是尚未初始化[尚未調(diào)用open方法]
1(初始化): 對象已建立厂置,尚未調(diào)用send方法
2(發(fā)送數(shù)據(jù)):send方法以調(diào)用菩掏,當時當前的狀態(tài)及http頭未知
3(數(shù)據(jù)發(fā)送中):以接受部分數(shù)據(jù),因為響應(yīng)及http頭不全昵济,這時通過responseBody和responseText獲取部分數(shù)據(jù)會出錯
4(完成): 數(shù)據(jù)接收完畢智绸,此時可以通過responseBody和responseText獲取完整的回應(yīng)數(shù)據(jù)
- responseBody: 以unsigned byted數(shù)組形式返回,只讀
- responseStream: 以Ado Stream對象的形式返回相應(yīng)信息访忿,只讀
- responseText: 以字符串形式返回瞧栗,只讀
- responseXML: 以XML Document對象形式返回,只讀
- status: 返回當前請求的http狀態(tài)碼海铆,只讀
100: Continue
101: Switching protocols
200: OK
201: Created
202: Accepted
203: Non-Authoritative Information
204: No Cotent
205: Reset Content
206: Partial Content
300: Multiple Choices
301: Moved Permanently
302: Found
303:See Other
304: Not Modified
305: Use Proxy
307: Temporay Redirect
400: Bad Request
401: Unauthorized
402: Payment Required
403: Forbidden
404:Not Fount
405: Method Not Allowed
406: Not Acceptable
407: Proxy Authentication Required
408: Request Timeout
409: Conflict
410: Gone
411: Length Required
412: Precondition Failed
413: Request Entity Too Large
414: Request-URI Too Long
415: Unsupported Media Type
416: Requested Range Not Suitable
417: Expectation Failed
500: Internal Server Error
501: Not Implemented
502: Bad Gateway
503: Service Unavailable
504: Gateway Timeout
505: HTTP Version Not Supported
- statusText: 放回當前請求的相應(yīng)行狀態(tài)迹恐,只讀
- 方法:
abort:取消當前請求
getAllResponseHeaders:獲取相應(yīng)的所有http頭
getResponseHeader:從響應(yīng)信息中獲得指定的http頭
xhr.getResponseHeader("Content-header");
open:創(chuàng)建一個新的http請求,并指定此請求的方法卧斟、URL以及驗證信息(用戶名/密碼)
xhr.open(get/post, url, isAsync, username, password);
send:發(fā)送請求到http服務(wù)器并接受回應(yīng)
setRequestHeader:單獨指定請求的某個http頭
POST發(fā)送請求時參數(shù)設(shè)置
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
數(shù)據(jù)放在send當中
xhr.send(key=value&key2=value2);
console
console.log();
console.warn();
console.debug();
console.info();
console.dir();//遍歷對象屬性
// 信息分組[開始的地方]
console.group('任務(wù)一');
console.groupEnd();
// 占位符
console.log("%d年%d月%d日",2011,3,26);
//查看對象的信息
console.dir(info);
//顯示某個節(jié)點的內(nèi)容
console.dirxml(info);
//判斷變量是否是真
console.assert(year == 2018 );
//打印函數(shù)的調(diào)用軌跡
function add(a,b){
console.trace();
return a+b;
}
var x = add3(1,1);
function add3(a,b){return add2(a,b);}
function add2(a,b){return add1(a,b);}
function add1(a,b){return add(a,b);}
面向?qū)ο髣?chuàng)建對象的方法
var Cat = {
createNew: function(){
var cat = {};
cat.name = "大毛";
cat.makeSound = function(){ alert("喵喵喵"); };
return cat;
}
};
var cat1 = Cat.createNew();
cat1.makeSound(); // 喵喵喵
工具-字符串操作
- .toUpperCase()
- .toLowerCase()