JSフロントエンドインタビューをすばやく取得する-第2章JSの基本-変数のタイプと計算

第2章JSの基本-変数の型と計算

1. JSの基本

1.値タイプと参照タイプ

2. typeof演算子

第二に、変数の計算と型変換

文字列の連結

2. ==および===

3. ifステートメントと論理計算

3.質問への回答の概要

 1. typeofはどのタイプを判断できますか?

 2. ==を使用する場合==を使用する場合?

 3.値タイプと参照タイプの違いは何ですか?

 4.手書きのディープコピー

4.まとめ


トピック紹介

  1. typeofはどのタイプを判断できますか?
  2. 使用する場合===使用する場合==?
  3. 値タイプと参照タイプの違いは何ですか?
  4. 手書きの深いコピー

1. JSの基本

1.値タイプと参照タイプ

値のタイプ

参照タイプ

a = 100とします

b = aとする

a = 200

Console.log(b)// 100

a = {年齢:20}とする

b = aとする

b.age = 21

Console.log(a.age)// 21

スタック分析:(スタックは上から下、ヒープは下から上)

値のタイプ

参照タイプ:

一般的な値のタイプ:数値、未定義、文字列、ブール、記号

一般的な参照タイプ:オブジェクト、配列特別な参照タイプ:null、fu()関数

2. typeof演算子

//すべての値タイプを決定します

みましょう; typeof a // ' undefined '

const str = ' abc ' ; typeof str // ' string '

const n = 100; typeof n // ' number '

const b = true; typeof b // ' boolean '

const s = Symbol('  s ' ); typeof s // ' シンボル'

//関数を判断できます

typeof console.log // ' function '

typeof function(){} // ' function '

//参照タイプを識別できます(識別を続行できません)

typeof null // ' オブジェクト'

typeof [ ' a ' ' b ' ] // ' オブジェクト'

typeof {x:100} // ' オブジェクト'

3.ディープコピー

浅いコピー

const obj1 = {
    age: 20,
    name: 'xxx',
    address: {
        city: 'beijing'
    }
}
const obj2 = obj1
obj2.address.city = 'shanghai'
console.log(obj1.address.city)   // shanghai

深いコピー

const obj1 = {
    age: 20,
    name: 'xxx',
    address: {
        city: 'beijing'
    },
    arr: ['a', 'b', 'c']
}
const obj2 = deepClone(obj1)
obj2.address.city = 'shanghai'   
obj2.arr[0] = 'a1'
console.log(obj1.address.city)   // beijing
console.log(obj1.arr[0])         // a
/** 
 * 深拷贝
 * @param {Object} obj 要拷贝的对象
 */
function deepClone(obj = {}) {
    if (typeof obj !== 'object' || obj == null) {
        // obj 是 null ,或者不是对象和数组,直接返回
        return obj
    }
    // 初始化返回结果
    let result
    if (obj instanceof Array) {    // 判断是否为数组
        result = []
    } else {
        result = {}
    }
    for (let key in obj) {
        // 保证 key 不是原型的属性
        if (obj.hasOwnProperty(key)) {
            // 递归调用!!!
            result[key] = deepClone(obj[key])
        }
    }
    // 返回结果
    return result
}


第二に、変数の計算と型変換

文字列の連結

const a = 100 + 10    // 110  可以使用100 + parseInt(‘10’)
const b = 100 + ‘10’   // 10010
const c = true + ‘10’  // true10	
const d = 100 + parseInt(‘10’)    // 110

2. ==および===

100 == ‘100’   // true
0 == ‘’   // true
0 == false   // true
false == ‘’   // true
null == undeined   // true

(==それらを等しくするようにしてください)

// 除了 == null 之外,其他都一律用 ===
Const obj = { x: 100 }
If (obj.a == null ) {}
// 相当于
// if (obj.a === null || obj.a === undefined) {}

3. ifステートメントと論理計算

本当に変数:!! a === true変数(2つの非操作の後でtrue)

誤って変数:!! a === false変数(2つの非操作の後はfalse)

//  一下是falsely变量,除此之外都是truly变量
!! 0 === false
!! NaN === false
!! ‘’ === false
!! null === false
!! undefined === false
!! false === false

3.質問への回答の概要

 1. typeofはどのタイプを判断できますか?

    すべての値タイプを特定する

    認識機能

    アプリケーションタイプかどうかを判断します(細分割できません)。

 2. ==を使用する場合==を使用する場合?

    == nullに加えて、他のすべての===

 3.値タイプと参照タイプの違いは何ですか?

    値タイプはディープコピー、参照タイプはシャローコピー

 4.手書きのディープコピー

    判定値型と参照型にご注意ください

    配列であるかオブジェクトであるかを判別するように注意してください

    再帰を使用する

4.まとめ

 1.値タイプVS参照タイプ、スタックモデル、ディープコピー

 2. Typeof演算子

 3.型変換、真と偽の変数

元の記事を26件公開 賞賛された6件 訪問数1391

おすすめ

転載: blog.csdn.net/Sabrina_cc/article/details/105449535