目錄
1.JS內建型別
2.型別轉換
3.運算子
4.原型,原型鏈,new
總結:
首頁 web前端 js教程 JS基礎知識必備—常見面試題目系統整理

JS基礎知識必備—常見面試題目系統整理

Aug 03, 2018 am 10:41 AM
css html javascript

JS基礎知識必備—常見面試題目系統整理

1.JS內建型別

分成基本資料型別與Object.
基本資料型別有:null,undefined,string,boolean,number,symbol .

    console.log(typeof null);//object
    console.log(typeof []);//object
    console.log(typeof {});//object
登入後複製

如果想要區分null,數組,對象,該怎麼辦?

       console.log(Object.prototype.toString.call(null));//[object Null]
       console.log(Object.prototype.toString.call([]));//[object Array]
       console.log(Object.prototype.toString.call({}));//[object Object]
登入後複製

我的簡單理解:toString(資料);作用:將資料轉換為字串。

推薦相關文章2020年最全js面試題整理(最新)

2.型別轉換

類型轉化:分為顯示型別轉化,與隱式型別轉換。

1.Number(資料)

如果数据内容式纯粹的数字,才可以转化为数字,否则式NaN。
登入後複製
var str2="12px";//NaN
var str2="1.2";//1.2
var str2="1.2.3";//NaN
var str2=null;//0
console.log(Number(str2));
登入後複製

#2.NaN

NaN的数据类型书Number。注意:NaN和任何东西都不相等,包括自己。
登入後複製

3.isNaN(資料)

会先把数据用Number转化,转化完了之后在判断是不是NaN,如果是NaN则返回为true。否则返回为fasle。
登入後複製
console.log(isNaN(1));//false
console.log(isNaN("123"));//false
console.log(isNaN("abc"));//true
登入後複製

4.parseInt(資料)和parseFloat(資料)

parseInt(数据):把数据变成整数,舍去小数点,取整数。
parseFloat(数据):把数据转化为数字,可以是小数。
注意:这两个方法会从左往右开始,除去空格,找到第一位非0数字,开始进行转换,直到转换到不是数字的那位为止,或者,转换出合适的值为止。
登入後複製
    console.log( parseInt( "1" ) );//1
    console.log( parseInt( "1.9" ) );//1
    console.log( parseInt( "20px" ) );//20
    console.log( parseInt( "    25px" ) );//25
    console.log( parseInt( "    0.0026px" ) );//0

    console.log( parseFloat( "    0.0026px" ) );//0.0026
登入後複製

5.Stirng( )和Boolean()也可以進行顯示類型轉化,這裡不綴述

条件判断中,除了null,undefined,'',NaN,false,0都转化为false,其余都转化为true。
登入後複製

6.隱式類型轉化

只有当加法运算时,其中一方是字符串类型,就会把另一个也转为字符串类型。其他运算只要其中一方是数字,那么另一方就转为数字。并且加法运算会触发三种类型转换:将值转换为原始值,转换为数字,转换为字符串。
登入後複製
<script>
    console.log( "abc"-1 );//NaN
    console.log( NaN-1 );//NaN
    console.log( NaN+1 );//NaN
    console.log( NaN+"1" );//NaN1
//------------------------------------------
    console.log( "3"-1 );//转成数字2
    console.log( "345" - 0 );//转成数字345
    console.log( "345" * 1 );//转成数字345
    console.log( "345" / 1 );//转成数字345
    console.log( "345px" - 0 );//NaN
//------------------------------------------
    
    console.log( 123 + "" );//转成字符串 "123"
//------------------------------------------
    
    console.log( !!234 );//转成boolean类型 true
    console.log( !!0 );//转成boolean类型 false
    console.log( !!{} );//转成boolean类型 true
    console.log( !!null );//转成boolean类型 false
登入後複製

3.運算子

==和= ==

==:
    1.如果类型相同的话,比较内容
        类型不相同,类型转化在比较
        1)一个是undefined,一个是null,则相等。
        2)数字和字符串的的话,将字符串转化为数字再进行比较。
===:
    1.类型相同比教内容,类型不同则返回fasle。
登入後複製

4.原型,原型鏈,new

#1.new

1.新生成了一个对象
2.链接到原型
3.绑定this
4.返回新对象
登入後複製

2.prototype 原型機

prototype 原型
    当一个函数被申明的时候,该函数下默认有一个属性:prototype,该属性的值是一个对象
    
    当我们去调用一个对象的属性或者方法的时候,如果该对象自身没有该属性或方法,
    则会调用到该对象 的 构造函数的prototype的属性或方法
    把通过构造函数构造出来的对象,共有的方法或者属性,放在prototype身上


__proto__
    当一个对象被创建的时候,
    该对象会自动被添加上一个属性:__proto__,
    他的值也是一个对象,并且该属性 就是 当前这个对象的构造函数的prototype
    对象.__proto__ === 构造函数.prototype
登入後複製

3.原型鏈

Function.prototype.a = "a";
Object.prototype.b = "b";
function Person(){}
console.log(Person);    //function Person()
let p = new Person();
console.log(p);         //Person {} 对象
console.log(p.a);       //undefined
console.log(p.b);       //b
登入後複製
解析:
会一直通过__proto__向上查找,最后当查找到Object.prototype时找到,最后打印出b,向上查找过程中,得到的是Object.prototype,而不是Function.prototype,找不到a属性,所以结果为undefined,这就是原型链,通过__proto__向上进行查找,最终到null结束
登入後複製
        //Function
        function Function(){}
        console.log(Function);  //Function()
        console.log(Function.prototype.constructor);    //Function()
        console.log(Function.prototype.__proto__);      //Object.prototype
        console.log(Function.prototype.__proto__.__proto__);    //NULL
        console.log(Function.prototype.__proto__.constructor);  //Object()
        console.log(Function.prototype.__proto__ === Object.prototype); //true
登入後複製

總結:

1.查找属性,如果本身没有,则会去__proto__中查找,也就是构造函数的显式原型中查找,如果构造函数中也没有该属性,因为构造函数也是对象,也有__proto__,那么会去它的显式原型中查找,一直到null,如果没有则返回undefined

2.p.__proto__.constructor  == function Person(){}

3.p.___proto__.__proto__== Object.prototype

4.p.___proto__.__proto__.__proto__== Object.prototype.__proto__ == null          

5.通过__proto__形成原型链而非protrotype
登入後複製

5.instanceof

检测对象原型是否在要检测对象的原型链上,返回值为true或者false
使用:object instanceof constructor
登入後複製
    function Fn () {}
    var f = new Fn();
    console.log( f instanceof Fn );//true
    console.log( f instanceof Object );//true
登入後複製
//    //str是字面量生成的,是由JS内部的String构造函数new出来的。
//    //但是str会立刻"压扁"自己,让自己不是对象。
//    //所以str都不是对象了,自然instanceof String 的得到的值为fasle
//    //但str.indexOf(),str还是可以调用indexOf()方法的原因是,当它调用方法的时候,会重新将自己包装成对象。
//    //使用结束后会重新"压扁"自己,让自己不是对象。
    var str = "123";
    console.log( str instanceof Array );//false
    console.log( str instanceof String);//false
    console.log( str instanceof Object);//false
//
    var obj = {};
    console.log( obj instanceof Array );//false
    console.log( obj instanceof Object);//true
登入後複製

6.hasOwnProperty

作用
        用来判断某个对象是否含有 指定的 自身属性
    语法
        boolean object.hasOwnProperty(prop)
    参数
        object
            要检测的对象
        prop
            要检测的属性名称。
    注意:不会沿着原型链查找属性,只查找自身属性
登入後複製
    function Fn(name,gender){
        this.name = name;
        this.gender = gender;
    }
    Fn.prototype.kind = "human";
    Fn.prototype.say = function(){
        console.log(1);
    };
    
    var f = new Fn("kimoo","男");
    
    console.log( f.hasOwnProperty("name") ); //true
    
    console.log( f.hasOwnProperty("kind") ); //false
    console.log( f.hasOwnProperty("say") ); //false
登入後複製

8.call,bind,apply

#call,bind,apply的作用都是修改this指向。

區別:

1.call:函数会立即执行,括号中的内容 第一个参数 就是 函数执行时候 ,内部的this指向。后面的参数就是函数内部的实参。
登入後複製
function foo (a,b) {
        console.log( this );
        console.log( a,b );
    }
    foo.call( document,2,3 );//运行结果:#document 2,3
登入後複製
2.apply:函数会立即执行,括号中的内容 第一个参数 就是 函数执行时候 ,内部的this指向。不过第二个参数接受数组。
登入後複製
    function foo (a,b) {
        console.log( this );
        console.log( a,b );
    }
    foo.apply( document,[2,3] ); // 和call 相似 直接调用 , 不过第二个参数接受数组。运行结果:#document 2,3
登入後複製
3.bind:函数 不会 立刻执行,返回的是 修改了 this指向的新函数.第二个参数是参数列表。
登入後複製
     function foo (a,b) {
        console.log( this );
        console.log( a,b );
    }
    var fn = foo.bind( document,2,3);// 函数 不会 立刻执行,返回的是 修改了 this指向的新函数
    fn();
       //运行结果:
    //#document
    //2 3
登入後複製

9.組合式繼承

<script>
    function Person(){
        this.arr = [1,2,3];
        this.age = 10;
    }
    Person.prototype.say = function(){
        console.log( "我会说话" );
    };
//    父类构造函数中的方法,使用借用构造函数的方式继承
    function Coder(name,money) {
        Person.call(this);
        this.name = name;
        this.money = money;
        
    }
//    父类构造函数原型上的方法,使用原型链继承
    Coder.prototype = new Person();
    Coder.prototype.constructor = Coder; // 重新修改 constructor
    
    Coder.prototype.say = function(){
        console.log( "我是程序员,我叫" + this.name +",我一个月:"+ this.money );
    }
    
    var p = new Person();
    console.log( p.age );  
    console.log( p.arr );
    console.log( p.kind );
    p.say();
    
    var c1 = new Coder("a",10000);
    console.log( c1.age );  
    c1.say();
    c1.arr.push(4);
    console.log( c1.arr );
    console.log( c1.name );
    console.log( c1.money );
    var c2 = new Coder("b",30000);
    c2.say();
    console.log( c2.age );  
    console.log( c2.arr );
    console.log( c2.name );
    console.log( c2.money );

    console.log(Coder.prototype.constructor);
</script>
登入後複製
10.拷貝式繼承

<script>
    
    function Person(){
        this.arr = [1,2,3];
        this.age = 10;
    }
    Person.prototype.say = function(){
        console.log( "我会说话" );
    }
    function Coder(){
        Person.call(this);
    }
    Coder.prototype = cloneFn( Person.prototype );
    Coder.prototype.constructor = Coder;
    Coder.prototype.say = function(){
        console.log( "我是程序员" );
    }
    
    var p = new Person();
    console.log( p.age );  
    console.log( p.arr );
    p.say();
    console.log( "----------------" );
    
    var c1 = new Coder();
    console.log( c1.age );  
    c1.say();
    c1.arr.push(4);
    console.log( c1.arr );
    console.log( "----------------" );
    var c2 = new Coder();
    console.log( c2.age );  
    console.log( c2.arr );
    c2.say();
    
//------------------------------------------
function cloneFn( sourse ){
    var o = Object.prototype.toString.call(sourse).toLowerCase().indexOf("array")!==-1 ? [] : {};
    for( var attr in sourse ){
        if( (typeof sourse[attr] === "object") && sourse[attr] !== null ){
            o[attr] = cloneFn( sourse[attr] ) ;
        }else{
            o[attr] = sourse[attr];
        }
    }
    return o;
}
    
</script>
登入後複製

11. class繼承

<script>
    class Person {
        constructor(name){
            this.name = name;
        }
        say(){
            console.log( &#39;我的名字叫 ${this.name}&#39;);
        }
    }
    
    class Coder extends Person{
//        constructor( name,money ){
//            super(name); // 相当于 调用 父类的 构造函数,修改内部的this,
////                一定要先调用 super 再使用 this
//            this.money = money;
//        }
        say(){
            console.log( `我的名字叫 ${this.name}` );
        }
        eat(){
            console.log( "我会吃" );
        }
    }
    
    var p = new Person("kimoo");
//    console.log( p );
//    p.say();
//    p.eat(); // 报错: p.eat is not a function
    
    var c = new Coder("zm");
    console.log( c );
    c.say();
    c.eat();
</script>
登入後複製
12.函數作用域,執行上下文,變數提升,閉包

1.作用域

JS作用域分为全局作用域和函数作用域,函数作用域可以访问全局作用域中的变量,对象,函数等。但是函数作用域外部访问不到函数内部的变量,对象,函数。

但在ES6中新增了块级作用域。let,const在块中声明的变量,函数等,外部都访问不到。
登入後複製
    {
        var a=1;
        let b=2;
        console.log(b);//2
        {
            console.log(b);//2
        }
    }

    console.log(a);//1
    console.log(b);//报错,b is not defined
登入後複製

#2 .執行上下文

這裡有一篇非常好的js執行上下文的文章,可以點擊連結查看#總結:

        1. 调用函数是会为其创建执行上下文,并压入执行环境栈的栈顶,执行完毕弹出,执行上下文被销毁,随之VO也被销毁
        2. EC创建阶段分创建阶段和代码执行阶段
        3. 创建阶段初始变量值为undefined,执行阶段才为变量赋值
        4. 函数申明先于变量申明
登入後複製
3 .變數提升(域解析)

關鍵:

變數提升過程中

函數優先權高於變數優先權

##
        function foo() {
            console.log(f1);    //f1() {}
            console.log(f2);    //undefined
            
            var f1 = &#39;hosting&#39;;
            var f2 = function() {}
            function f1() {}
        }
        foo();
登入後複製

4.閉包

閉包:函數A 傳回了一個函數B,並且函數B 中使用了函數A 的變量,函數B 就被稱為閉包。

   for ( var i=1; i<=5; i++) {
    setTimeout( function timer() {
        console.log( i );
    }, i*1000 );
}
登入後複製

首先因為 setTimeout 是個非同步函數,所有會先把迴圈全部執行完畢,這時候 i 就是 6 了,所以會輸出一堆 6。

解決方法一:

for (var i = 1; i <= 5; i++) {
  (function(j) {
    setTimeout(function timer() {
      console.log(j);
    }, j * 1000);
  })(i);
}
登入後複製

解決方法二:

for ( var i=1; i<=5; i++) {
    setTimeout( function timer(j) {
        console.log( j );
    }, i*1000, i);
}
登入後複製
解決方法三:

for ( let i=1; i<=5; i++) {
    setTimeout( function timer() {
        console.log( i );
    }, i*1000 );
}
登入後複製

點擊這裡,閉包進一步了解

13.深淺拷貝

1.淺拷貝

#首先可以透過Object.assign 來解決這個問題。

let a = {
    age: 1
}
let b = Object.assign({}, a)
a.age = 2
console.log(b.age) // 1
登入後複製

當然我們也可以透過展開運算子(…)來解決###
let a = {
    age: 1
}
let b = {...a}
a.age = 2
console.log(b.age) // 1
登入後複製
######2.深拷貝######
function cloneFn( sourse ){
    var o = Object.prototype.toString.call(sourse).toLowerCase().indexOf("array")!==-1 ? [] : {};
    for( var attr in sourse ){
        if( (typeof sourse[attr] === "object") && sourse[attr] !== null ){
            o[attr] = cloneFn( sourse[attr] ) ;
        }else{
            o[attr] = sourse[attr];
        }
    }
    return o;
}
登入後複製
###相關文章:### #########JS的8個必須注意的基礎知識############javascript基礎整理#######

以上是JS基礎知識必備—常見面試題目系統整理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

vue中怎麼用bootstrap vue中怎麼用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

HTML,CSS和JavaScript的角色:核心職責 HTML,CSS和JavaScript的角色:核心職責 Apr 08, 2025 pm 07:05 PM

HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

React在HTML中的作用:增強用戶體驗 React在HTML中的作用:增強用戶體驗 Apr 09, 2025 am 12:11 AM

React通過JSX與HTML結合,提升用戶體驗。 1)JSX嵌入HTML,使開發更直觀。 2)虛擬DOM機制優化性能,減少DOM操作。 3)組件化管理UI,提高可維護性。 4)狀態管理和事件處理增強交互性。

了解HTML,CSS和JavaScript:初學者指南 了解HTML,CSS和JavaScript:初學者指南 Apr 12, 2025 am 12:02 AM

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

bootstrap怎麼設置框架 bootstrap怎麼設置框架 Apr 07, 2025 pm 03:27 PM

要設置 Bootstrap 框架,需要按照以下步驟:1. 通過 CDN 引用 Bootstrap 文件;2. 下載文件並將其託管在自己的服務器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根據需要編譯 Sass/Less;5. 導入定製文件(可選)。設置完成後,即可使用 Bootstrap 的網格系統、組件和样式創建響應式網站和應用程序。

bootstrap怎麼寫分割線 bootstrap怎麼寫分割線 Apr 07, 2025 pm 03:12 PM

創建 Bootstrap 分割線有兩種方法:使用 標籤,可創建水平分割線。使用 CSS border 屬性,可創建自定義樣式的分割線。

bootstrap怎麼插入圖片 bootstrap怎麼插入圖片 Apr 07, 2025 pm 03:30 PM

在 Bootstrap 中插入圖片有以下幾種方法:直接插入圖片,使用 HTML 的 img 標籤。使用 Bootstrap 圖像組件,可以提供響應式圖片和更多樣式。設置圖片大小,使用 img-fluid 類可以使圖片自適應。設置邊框,使用 img-bordered 類。設置圓角,使用 img-rounded 類。設置陰影,使用 shadow 類。調整圖片大小和位置,使用 CSS 樣式。使用背景圖片,使用 background-image CSS 屬性。

bootstrap按鈕怎麼用 bootstrap按鈕怎麼用 Apr 07, 2025 pm 03:09 PM

如何使用 Bootstrap 按鈕?引入 Bootstrap CSS創建按鈕元素並添加 Bootstrap 按鈕類添加按鈕文本

See all articles