preface

  • Online music poke me ah!
  • Music blog source code online!
  • If you want to do something in your spare time recently, you can’t step forever. You should rush forward and say something realistic. The company doesn’t want you tomorrow, you can find the next one the day after tomorrow.
  • Muddlehead in the front-end field bump bump for more than two years, want to see Vue source code, do not know if there is recently want to see the source of ape friends, if JS is not hard enough, suggest to re-learn JS with me, re-learn to believe that to see the source code, will get twice the result with half the effort.
  • Next, let’s look at what the JS data type knowledge points can be tested.

Today, I went to a company for an interview, and the interviewer said, “Give me five seconds to remember you.” Then I swung around and gave him a slap, and then I left. Just as I got home, the interview came. You said… I go to?

Start by asking yourself three interview questions

Undefined is null.

When do I assign a variable to null?

What do variable types and data types stand for?

If you do, the interviewer is no match for you. So, let’s take a few minutes and look at the following questions.

What are the data types?

Some say six, some say eight.

In fact, the correct answer is 8, in the ES5 era is indeed 6, but in the ES6 era added Symbol, ES10 era added bigInt, these two types, can be counted together seven brothers.

Data types fall into two broad categories.

  • Basic (value) types
    • String: Arbitrary string
    • Number: Arbitrary number.NaN, InfinityAlso a special numeric value of a number type
    • Boolean:true / false
    • undefined: undefined
    • null:null
    • symbol: represents a unique value. Objects of this type are never equal, so the same value is passed in when they are created. This can solve the problem of conflicting attribute names, as a marker. Generated by the Symbol function call, because the generated Symbol value is the original type, soSymbolFunction cannot be usednewThe call.
    • bigInt: more than2^53 - 1Any integer of. Google version 67 already supports this type.
  • Object (reference) type
    • Object: arbitrary object
      • Function: a special object (executable)
      • Array: a special object (numeric subscript, internal data ordered)
      • Date: a special object (for dealing with dates and times)

How to determine the type?

Typeof (return string, typeof value)

Undefined/Number/String/Boolean/function Null/Object/ArrayCopy the code

Instanceof (return to Boolean)

Determine the specific type of an object (array object functions are special representations of objects)Copy the code

Constructor (return value type, not string)

Determine the specific type of object (array, object, function, re). Each object has a constructor property that references the constructor that initializes the object.Copy the code

=== (return Boolean)

Undefined/null (because both types have only one value, which is itself)Copy the code

Treasure, the knowledge point above metropolis, go up a few topic test, you should no problem.


var a;
console.log(a, typeof a, typeof a==='undefined',a===undefined );  // undefined 'undefined' true true
console.log(undefined= = ='undefined');  // false

a = null
console.log(typeof a, a===null) // 'object' true

// Typeof returns a string
// undefined and null are congruent === = true

var b1 = {
    b2: [1.'abc'.console.log],
    b3: function () {
      console.log('b3')
      return function () {
        return 'zzm'}}}console.log(b1 instanceof Object, b1 instanceof Array) // true false
console.log(b1.b2 instanceof Array, b1.b2 instanceof Object) // true true
console.log(b1.b3 instanceof Function, b1.b3 instanceof Object) // true true

// Instanceof is an object, but not necessarily an array or function, but conversely, a function or array must be an object

console.log(typeof b1.b2, '-- -- -- -- -- -- --) // 'object'

// instanceof whether you are an array or a function is 'object'

console.log(typeof b1.b3==='function') // true

// You can see typeof as well as function in the object

console.log(typeof b1.b2[2= = ='function')  // true
b1.b2[2] (4)  //  4
console.log(b1.b3()())  // zzm
Copy the code

Interviewer: Number(‘ ZZZ ‘) == NaN?

Must befalse.Number('zzz') the outputNaN.Number('zzz') the outputNaNNaN= =NaNWhy is itfalse?'How handsome are you?'NaNRight,'Give it a thumbs up.'Is alsoNaNOkay, so are they worth the same? It's definitely not the same, so it's not equal.Copy the code

Use isNaN() to test for non-numeric types. Returns the value Boolean.

Other methods of various data types

methods Does not support instructions example
isNaN() Whether the type is non-numerical isNaN(NaN); true
Number() Convert the other types to number Number(“18”); 18
isFinite() Check if it’s Infinity. isFinite(1/0); false
Boolean() Cast the value to Boolean Boolean(0); false
parseInt() Parses the string and returns an integer parseInt(“18”); 18
parseFloat() Parses the string and returns a floating point number ParseFloat (40.56 years); 40.56
toString() Null, and undefined Convert other types to strings let a = true; a.toString(); “true”
toLocaleString() Convert an array to a local string let arr=[‘1′,’2’]; arr.toLocaleString(); “1, 2,”
isArray() Verify that the value is an array let arr=[‘1′,’2’]; arr.isArray(); true

Ok, to answer the question from the beginning.

Interviewer:undefinedwithnullThe difference between?

Undefined means the definition has no value assigned. Undefined is derived from null.

Nul is defined and assigned, but the value is null.

Interviewer: When do you assign a variable to null?

Initial assignment, indicating that an object will be assigned;

Before you finish, make the object garbage (to be collected by the garbage collector).

Interviewer: Strictly distinguish variable types from data types?

  • Type of data
    • Basic types of
    • Object type
  • The type of the variable (the type of the variable’s memory value)
    • Basic type: Save data that is the basic type
    • Reference type: Holds the address value

In the past to recommend

Vue-cli3 builds the component library

Vue implements dynamic routing (and the interviewer blows project highlights)

Axios manipulation in a project you didn’t know about (Core principles of handwriting, compatibility)

VuePress builds project component documentation

Vue koa2 + + nginx deployment

Vue-typescript-admin-template background management system

The original link

Juejin. Cn/post / 699776…