javascript
javascript基础(第一天)
前面一句話: 挖掘知識的邊界,摳的越細,深度和廣度才越高,才越牛.
js的性能已經達到java的五分之一
(1) == ===的區別??
? ? ??null == undefined //true
? ? ??'' == false //true
? ? ??總結 ==不靠譜, 只用===
(2) 不使用continue;
? ? ??效率低,出處:javascript語言精粹111頁
(2) var a = {};
? ? ? a.b='xx';
? ? ??a['delete'] = 'yy'; ///關鍵詞也無所謂了
(3) delete 刪屬性
? ? ??全局變量和局部變量刪除不掉???
? ? ??var aaaa = 123;
? ? ??delete aaaa;
? ? ??console.log(aaaa); //123
? ? ? window.aaaa = 123;
? ? ??delete window.aaaa;
? ? ??console.log(window.aaaa); //undefined
? ? ??function a(){
? ? ??? ? ??var a= 123;
? ? ??? ? ??delete a;
? ? ??? ? ??console.log(a); //123
? ? ??}a();
? ? ?設為a=null;祈禱上天,等待垃圾回收機制回收.
? ? ?注:當使用var聲明一個變量時,創建的這個屬性是不可配置的,也就是說無法通過delete運算符刪除
? ? ?var name=1 ? ?->不可刪除
? ? ?sex=”girl“ ? ? ? ? ->可刪除
? ? ?this.age=22 ? ?->可刪除
(4) typeof instanceof
? ? ??typeof 2 === 'number' //true
? ? ??typeof new Number(2) === 'number' //false
? ? ??typeof '2' === 'string' //true
? ? ??typeof new String('2') === 'string' //false
? ? ??typeof true === 'boolean' //true
? ? ??typeof new Boolean(true) === 'boolean' //false
? ? ??typeof [] === 'object' //true
? ? ??var a = function(){}; typeof a === 'function'; //true
? ? ??[] instanceof Object //true
? ? ??2 instanceof Number //false
? ? ??new Number(2) instanceof Number //true
? ? ??var a = function(){}
? ? ??var b = function(){};
? ? ??a.prototype = new b();
? ? ??new a() instanceof b; //true
? ? ??// typeof 能判斷 基本類型 和 function
? ? ??// instanceof 能判斷 自定義對象
? ? ??//擴展 Object.prototype.toString.call([]) === '[object Array]'; 這樣是最棒的判斷
(4) if語句
? ? ??if( xxxx ) ? //6種情況為false
? ? ? // null undefined NaN 0 '' false
(4) for in?
? ? ??//遍歷對象
? ? ? //遍歷松散的數組
? ? ? var a = new Array(10); a[2]=10; a[5]=10; for(var i in a){ console.log(i);?}
? ? ? ?性能,確實優化了
? ? ??var a = new Array(10000000);
? ? ??a[2]=10;
? ? ??a[5]=10;?
? ? ??var time1 = new Date().getTime();
? ? ??for(var i in a ){?
? ? ??? ? ??Math.random()* Math.random()* Math.random() * Math.random();
? ? ??}
? ? ??var time2 = new Date().getTime();
? ? ??for(var i=0; i<a.length; a++){?
? ? ??? ? ??Math.random()* Math.random()* Math.random() * Math.random();
? ? ??}
? ? ??var time3 = new Date().getTime();
? ? ??console.log(time2-time1); //~100ms
? ? ??console.log(time3-time2); //~200ms
(5) 函數字面量 與 函數聲明
? ? ??var a = 2;
? ? ??function a(){}
? ? ??console.log(typeof a); //function ? number ?
?
? ? ??總結:
? ? ??變量的聲明被提前到作用域頂部,賦值保留在原地;
? ? ??聲明式函數整個“被提前”;
? ? ??函數表達式只有變量“被提前”了,函數沒有“被提前”;
? ? ??函數的聲明比變量的聲明具有高的優先級。
? ? ??賦值會根據前后順序進行覆蓋!
? ? ??var a = function(){
? ? ??? ? ??console.log('aaa');
? ? ??}
? ? ??function a(){
? ? ??? ? ??console.log('bbb');
? ? ??}
? ? ??//var b = function b(){} === function b(){}
? ? ??a();
(6) 函數返回值
? ? ??var a = function(b,c){
? ? ??? ? ??console.log(b+','+c);
? ? ??}
? ? ??console.log( a() );
(7) this指向
? ? ??function a(){
? ? ? ? ? ??var that = self = this;
? ? ? ? ? ? this.v = 'aaa';
? ? ??? ? ??function b(){
? ? ??? ? ??? ? ??console.log(this);
? ? ? ? ? ?? ? ?? console.log(this.v);
? ? ?? ? ???}
? ? ??? ? ??b();
? ? ??}
? ? ??new a();
(8) arguments
? ? ??function a(a,b,c,d){
? ? ??? ? ??//var args = [].slice.call(arguments);
? ? ??}
(9) apply call
? ? ??function a(){
? ? ??? ? ??console.log(this.x+this.y);
? ? ??}
? ? ??a.call({x:100,y:100});
? ? ??function C(){
? ? ??? ? ??this.m = function(){
? ? ??? ? ??? ? ??console.log(this.x+this.y);?
? ? ??? ? ??}
? ? ??}
? ? ??var c = new C();
? ? ??c.m.call({x:100,y:100});
? ? ??//這個就是this的重定向,原諒我吧,沒法解釋的很準確和淺顯易懂,只能多練習
(10) try catch throw 關鍵邏輯加上就好
? ? ??var a = 'aaa';
? ? ??try{
? ? ??? ? ??a = xxxx(); //不存在的方法
? ? ??}catch(e){
? ? ??? ? ??throw(new Error('error'));
? ? ??? ? ??a = 0;
? ? ??}
? ? ??console.log("go go go"); //這里走嗎???
(11) 閉包
? ? ??for(var i=0; i<2; i++){
? ? ??}
? ? ??console.log(i);
? ? ??function a(){
? ? ??? ? ??//只有方法體的才有作用域
? ? ??}
? ? ??
? ? ??var a;
? ? ??function c(){
? ? ??var v = 'vvv';
? ? ??function b(){
? ? ??? ? ??return {v:v};
? ? ??};
? ? ??a = b();
? ? ??}
? ? ??c();
? ? ??console.log(a.v);?//方法體是作用域, 這個大家都知道了, 內部的方法體需要給個口子出來,讓外面可以訪問到.
(12) 回調
? ? ??function a(callback){
? ? ??? ? ??callback();
? ? ??}
? ? ??a(function(){
? ? ??? ? ??console.log('aaa');
? ? ??});
(13) 自執行函數, 也叫自調函數
? ? ??(function(a){
? ? ??? ? ??console.log(a);
? ? ??})('aaa');
(14) 正則
? ? ??var regexp = / /img
? ? ??? ? ??//i ? 大小寫
? ? ??? ? ??//m 換行
? ? ? ? ? ? //g ?檢索到第一個匹配不停止,檢索全部
(15) prototype
? ? ??//prototype是什么?
? ? ??function a(){
? ? ??? ? ??this.v='vvv';
? ? ??}
? ? ??console.log(a.prototype); //當前對象,函數也是對象,
? ? ??console.log(a.prototype.constructor); //當前方法
? ? ??console.log(a.prototype.constructor.__proto__ === a.__proto__); //true
? ? ??//prototype是函數的內置屬性,__proto__是對象的內置屬性,都是查找原型鏈的
? ? ??//prototype使用場景?
? ? ??var b = {};
? ? ??//function b(){};
? ? ??//var b = function(){}
? ? ??b.prototype.bbb='bbb';
? ? ??console.log(b.bbb);
? ? ??//console.log(new b().bbb); //構造器
? ? ??//prototype咋玩? (下面的都是不好的用法)
? ? ??function a(){};
? ? ??a.prototype = function(){ console.log("aaa") };
? ? ??console.log( new a.prototype() );
? ? ??// function a(){}
? ? ??// function b(){}
? ? ??// a.prototype = {aaa:'aaa'};
? ? ??// b.prototype = a.prototype;
? ? ??// var aa1 = new a(),
? ? ??// aa2 = new a(),
? ? ??// bb1 = new b();
? ? ??// aa1.prototype.aaa='xxx';
? ? ??// console.log( aa2.aaa );
? ? ??// console.log( bb1.aaa );
? ? ??// function a(){}
? ? ??// function b(){ this.bbb='bbb'};
? ? ??// a.prototype = new b();
? ? ??// console.log(new a().bbb );
? ? ??//-----4 其實是這樣的
? ? ??Object.create();
最后一句話: !!!只有最佳技術實踐才能救命!!!
轉載于:https://www.cnblogs.com/tm-roamer/p/4540423.html
總結
以上是生活随笔為你收集整理的javascript基础(第一天)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UESTC_摩天轮 2015 UESTC
- 下一篇: java个人学习笔记:取模运算 整数除运