變量的基本類型 原始類型:number,string,boolean,null,undefined 引用類型:Object,Array,Function typeof 返回的值(返回的是字符串) number,string,boolean,object,undefined,function {} [] null 返回的是object 使用函數判斷具體數據類型: function uniq(item){ var itemType = Object.prototype.toString.call(item) if (typeof item == 'object') { if(item==null){ console.log('null') }else if(itemType =="[object Array]"){ console.log('Array') }else{ console.log('Object') } }else{ console.log(typeof item) } } var str = undefined uniq(str) instanceOf 判斷變量是屬於哪一種類型,返回的值是boolean 強制類型轉換 字符串拼接 == 賦值語句 邏輯運算 if語句 === == 若是判斷值是不是 null和undefined if(obj.a == null){} 等價於 if(obj.a === null||obj.a===undefined){} js中的內置函數 Object,Array,Boolean,Number,String,Function,Date,RegExp,Error 如何理解JSON JSON是JS的一個對象 JSON.stringify({a:10,b:20}) JSON.parse('{"a":10,"b":20}') 原型和原型鏈: 全部的引用類型都具備對象特色,即自由擴展屬性 全部的函數都有一個顯示類型的屬性,prototype指向構造器 全部的引用類型都有一個_proto_屬性,屬性只是一個普通對象 new 一個函數的過程 新建一個對象 this指向這個對象 給this賦值, 返回this prototype 引用 Father.prototype.lastName = 'xiaogeng' function Father(name,age){ this.name = name; this.age = age; } var father = new Father('genggeng',25) Son.prototype = father Son.prototype.car = 'benz' function Son(grade){ this.grade = grade } var item for(item in f){ if(f.hasOwnProperty(item)){ console.log('是自己屬性') } } var son = new Son('3年級'); console.log(son); 繼承 原型鏈繼承 Grande.prototype.lastName = "benz" function Grande(name,age){ this.name = name; this.age = age } var grande = new Grande() Father.prototype = grande; function Father(grade){ this.grade = grade } var father = new Father("xiaogeng",24,'大四') console.log(father) call/apply借用:該方法的缺點是不能繼承原型上的東西 Grande.prototype.lastName = "benz" function Grande(name,age){ this.name = name; this.age = age } function Father(name,age,grade){ Grande.call(this,name,age) this.grade = grade; } var father = new Father('xiaogeng',24,'大四') console.log(father) 共享原型:不能修改原型 Grande.prototype.lastName = "benz" function Grande(){ } function Father(){ } function inhert(Tagert,Origin){ Tagert.prototype = Origin.prototype } inhert(Father,Grande); var father = new Father() 聖盃模式 Grande.prototype.lastName = "benz" function Grande(){ } function Father(){ } function inhert(Tagert,Origin){ function F(){} F.prototype = Origin.prototype Tagert.prototype = new F() Tagert.prototype.constructor = Tagert //把Target的構造函數指向歸位 Tagert.prototype.uber = Origin.prototype //爲了讓咱們知道Target真正繼承自誰 } inhert(Father,Grande); var father = new Father()
function test(){ //函數聲明 } var text = function(){ //函數表達式 } //在後面添加執行符,函數表達式是能夠執行的,可是函數聲明不能被執行,由於函數聲明會被先提高 //執行期上下文:每一個javascript函數都是一個對象,對象中有些屬性咱們能夠訪問,但有些不能夠,這些屬性僅供JavaScript引擎存取[[scope]]就是其中一個;[[scope]]就是我所說的做用域,其中存儲了運行期上下文的集合;[[scope]]中存儲的執行期上下文對象的集合,這個集合呈鏈式鏈接,咱們把這種鏈式鏈接叫作做用域鏈 //範圍:一段script標籤或者函數內,會產生一個執行期上下文 //全局:函數定義,函數聲明 //函數:變量定義,函數聲明、this、arguments this //指向在定義時沒法肯定,在執行時才能確認 var a = { name:'a', fn:function(){ console.log(this.name) } } a.fn() a.fn.call({name:'b'}) var fn1 = a.fn fn1() //this指向window // 做爲構造函數執行,this指向new出來的對象 // 做爲普通函數執行,this指向window // call和apply、bind,this指向被綁定的對象 // 對象中的this,指向最近調用的一級 //做用域:javascript沒有塊級做用域 if(true){ var a = 2; //不推薦這種寫法,由於這一樣是定義在全局的,可是不易讀 } //js的做用域分爲全局做用域和函數做用域,爲了防止污染全局變量,函數當定義在函數做用域內部 //做用域鏈: var a = 100; function fn(){ var b = 50; function fn2(){ var c = 300 console.log(a) //a是自由變量 console.log(b) //b是自由變量 console.log(c) } }
// 同步和異步 //"同步模式"就是上一段的模式,後一個任務等待前一個任務結束,而後再執行,程序的執行順序與任務的排列順序是一致的、同步的.是一種阻塞模式 // "異步模式"則徹底不一樣,每個任務有一個或多個回調函數(callback),前一個任務結束後,不是執行後一個任務,而是執行回調函數,後一個任務則是不等前一個任務結束就執行,因此程序的執行順序與任務的排列順序是不一致的、異步的。是非阻塞的 //異步的使用場景: 須要等待就須要異步 //定時任務: setTimeOut,setInterval //網絡請求: ajax請求,動態加載 //事件
var today = new Date(); today.getTime() //獲取毫秒數 today.getFullYear() //年 today.getMouth() // 月(0-11) today.getDate() // 日(0-31) today.getHours() //小時(0-23) today.getMinutes() //分鐘(0-59) today.getSeconds() // 獲取秒