javascript
javascript基础(第一天)
前面一句話: 挖掘知識(shí)的邊界,摳的越細(xì),深度和廣度才越高,才越牛.
js的性能已經(jīng)達(dá)到j(luò)ava的五分之一
(1) == ===的區(qū)別??
? ? ??null == undefined //true
? ? ??'' == false //true
? ? ??總結(jié) ==不靠譜, 只用===
(2) 不使用continue;
? ? ??效率低,出處:javascript語言精粹111頁
(2) var a = {};
? ? ? a.b='xx';
? ? ??a['delete'] = 'yy'; ///關(guān)鍵詞也無所謂了
(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();
? ? ?設(shè)為a=null;祈禱上天,等待垃圾回收機(jī)制回收.
? ? ?注:當(dāng)使用var聲明一個(gè)變量時(shí),創(chuàng)建的這個(gè)屬性是不可配置的,也就是說無法通過delete運(yùn)算符刪除
? ? ?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 能判斷 自定義對(duì)象
? ? ??//擴(kuò)展 Object.prototype.toString.call([]) === '[object Array]'; 這樣是最棒的判斷
(4) if語句
? ? ??if( xxxx ) ? //6種情況為false
? ? ? // null undefined NaN 0 '' false
(4) for in?
? ? ??//遍歷對(duì)象
? ? ? //遍歷松散的數(shù)組
? ? ? var a = new Array(10); a[2]=10; a[5]=10; for(var i in a){ console.log(i);?}
? ? ? ?性能,確實(shí)優(yōu)化了
? ? ??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) 函數(shù)字面量 與 函數(shù)聲明
? ? ??var a = 2;
? ? ??function a(){}
? ? ??console.log(typeof a); //function ? number ?
?
? ? ??總結(jié):
? ? ??變量的聲明被提前到作用域頂部,賦值保留在原地;
? ? ??聲明式函數(shù)整個(gè)“被提前”;
? ? ??函數(shù)表達(dá)式只有變量“被提前”了,函數(shù)沒有“被提前”;
? ? ??函數(shù)的聲明比變量的聲明具有高的優(yōu)先級(jí)。
? ? ??賦值會(huì)根據(jù)前后順序進(jìn)行覆蓋!
? ? ??var a = function(){
? ? ??? ? ??console.log('aaa');
? ? ??}
? ? ??function a(){
? ? ??? ? ??console.log('bbb');
? ? ??}
? ? ??//var b = function b(){} === function b(){}
? ? ??a();
(6) 函數(shù)返回值
? ? ??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});
? ? ??//這個(gè)就是this的重定向,原諒我吧,沒法解釋的很準(zhǔn)確和淺顯易懂,只能多練習(xí)
(10) try catch throw 關(guān)鍵邏輯加上就好
? ? ??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);?//方法體是作用域, 這個(gè)大家都知道了, 內(nèi)部的方法體需要給個(gè)口子出來,讓外面可以訪問到.
(12) 回調(diào)
? ? ??function a(callback){
? ? ??? ? ??callback();
? ? ??}
? ? ??a(function(){
? ? ??? ? ??console.log('aaa');
? ? ??});
(13) 自執(zhí)行函數(shù), 也叫自調(diào)函數(shù)
? ? ??(function(a){
? ? ??? ? ??console.log(a);
? ? ??})('aaa');
(14) 正則
? ? ??var regexp = / /img
? ? ??? ? ??//i ? 大小寫
? ? ??? ? ??//m 換行
? ? ? ? ? ? //g ?檢索到第一個(gè)匹配不停止,檢索全部
(15) prototype
? ? ??//prototype是什么?
? ? ??function a(){
? ? ??? ? ??this.v='vvv';
? ? ??}
? ? ??console.log(a.prototype); //當(dāng)前對(duì)象,函數(shù)也是對(duì)象,
? ? ??console.log(a.prototype.constructor); //當(dāng)前方法
? ? ??console.log(a.prototype.constructor.__proto__ === a.__proto__); //true
? ? ??//prototype是函數(shù)的內(nèi)置屬性,__proto__是對(duì)象的內(nèi)置屬性,都是查找原型鏈的
? ? ??//prototype使用場(chǎng)景?
? ? ??var b = {};
? ? ??//function b(){};
? ? ??//var b = function(){}
? ? ??b.prototype.bbb='bbb';
? ? ??console.log(b.bbb);
? ? ??//console.log(new b().bbb); //構(gòu)造器
? ? ??//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 其實(shí)是這樣的
? ? ??Object.create();
最后一句話: !!!只有最佳技術(shù)實(shí)踐才能救命!!!
轉(zhuǎn)載于:https://www.cnblogs.com/tm-roamer/p/4540423.html
總結(jié)
以上是生活随笔為你收集整理的javascript基础(第一天)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UESTC_摩天轮 2015 UESTC
- 下一篇: java个人学习笔记:取模运算 整数除运