Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Reviewed some JS the most basic knowledge about

  • Classification and judgment of data types πŸŽ‰ and some extended basics

  • Data, variables, memory understanding πŸ€”

    • Basic knowledge of data storage 😴

    • What is assigned to a variable when you assign it a value? 🧐

    • Is the argument passed in when a function is called a value or a reference address? 😐

This is pretty simple but it’s hard to understand the first time you study it

But this knowledge will be very helpful in the later learning prototype chain and other in-depth knowledge!

So ~ still want to consolidate the foundation well

If have what not comprehensive/appear wrong place welcome everybody big guy’s discussion/correct ❀️ ramming foundation blunt blunt!

Classification and judgment of data types

1. Data type classification and judgment πŸŽ‰

What is data? πŸ‘“

First of all, what is data?

Data is something that’s in memory that’s readable and passable that holds certain information and it’s essentially binary machine code and what we programmers see must be high-level language and we write code that contains data that’s compiled so that the computer can understand it and perform various operations.

Everything is data functions, objects, everything is data

In memory all of our operations are targeted at data such as

  • Arithmetic operations

  • Logical operations

  • The assignment operation

  • Run the function

Doing all this involves using data types (hey! Let’s take a look at the categories of data types and how to identify them!

Classification and judgment of data types πŸ”

As you all know, there are two types of data — primitive types and object reference types. Let’s list them

Basic data

  • Number Number type

  • String String type

  • Boolean Indicates the Boolean value type

  • undefined

  • null

How to judge the above five types? Use typeof?

Yes but not entirely because null is not able to judge der~

The data type Judgment method
numeric typeof
String type typeof
The Boolean typeof
undefined Typeof or = = =
null = = =

There’s an extra bit of === here that means strict congruence doesn’t automatically convert the data types on both sides of the equation

And == will automatically implicitly cast both sides of the equation

So for the sake of rigor we pursue is congruent use === (I know we all know I just mention a mouth 🀐)

var a = Awesome!;
var b = 'bill';
var c = true;
var d;
var e = null;
console.log(typeof a);// number
console.log(typeof b);// string
console.log(typeof c);// boolean
console.log(typeof d, d === undefined);// undefined true
console.log(typeof e, e === null);// object(typeof cannot be used for null) true
Copy the code

Note here that typeof A returns the string ao

var a = Awesome!;
var d = undefined; 
console.log(typeof a, typeof a === 'number');// number true
console.log(d === undefined.typeof d === undefined.typeof d === 'undefined');// true false true
Copy the code

Object (reference) data

  • Object

  • Array (a special object

  • The Function Function

The data type Judgment method
Object typeof/instanceof
Array instanceof
Function typeof
var a = {};
var b = new Array(6);
var c = () = > {};

console.log(typeof a, a instanceof Object);// object true
console.log(typeof b, b instanceof Array);// object() true
console.log(typeof c, c instanceof Function);// function true
Copy the code

【 simulation interview 】undefined null those things ✨

Interviewer: “What’s the difference between null and undefined?”

At this time we can expand to talk about ~

In simple terms

  • Undefined means the definition has no value assigned

  • Null means defined and assigned but assigned a NULL

So why null? What does null do? (A further point πŸ˜‚)

var obj = null;// 01 means we are going to assign the variable obj to object ~ (array, object, function are ok)
obj = ['bill'.21];
obj = null;// 02 makes the object pointed to by obj garbage (the garbage collector will come and collect it to prevent memory consumption ~)
Copy the code

As above πŸ‘†

  • 01 Initial assignment indicates that obj will be assigned as an object
  • 02 Make objects garbage after using obj

2. Understanding of data, variables and memory πŸ€”

Basic knowledge of data storage 😴

Before you understand the basics of data storage, you need to understand variables and memory. They are closely related

  • variable

A variable corresponds to a small chunk of memory in which the value of the variable is stored!

  • memory

Storage space generated after the memory module is powered on (not available after temporary power failure)

The memory stick is this green thing 🧐

A block of memory contains two aspects of data

  • Data stored internally (stored in heap space – heap space for storing objects takes up a lot of space)

  • Address value data (stored in stack space – stack space is used to store variables in a smaller space)

For example

var obj = {name:'Tom'}
Copy the code

Essentially, the memory contents of the object to the right of the equals sign (that is, its address value) are assigned to the variable obj

The line on the left of πŸ‘‡ is the simulated stack ~ the heap on the right

This reminds me of what I learned in Java when I studied the underlying mechanism of the OBJECT-ORIENTED JVM

This is the notes that I studied at that time the notes on XD object oriented foundation

In the following figure, cat is the variable (in the stack) that points to the object (in the heap) created by new cat () ~

What is assigned to a variable when you assign it a value? 🧐

Let’s look at an example

// 01 Assign basic data to the variable -- this is the basic data saved
var a = Awesome!;
// 02 Assign a variable to an object -- store the address of the object.
var obj = {name: "Bill"};
// 03 Important! Obj1 obj2 a obj
// -- Memory space can hold either basic data (a) or object address (obj).
var obj1 = a;
var obj2 = obj;
Copy the code

There are three distinct cases

  • [1] It can be given the basic data type as 01

  • [2] It can be assigned the address value of the object as 02

  • [3] Assign it the memory contents of an object as 03 does

    • This memory content can be a primitive data type
    • It can also be the address of an object

Is the argument passed to a function a value or a reference address? 😐 is “value passing/reference passing?”

Say first conclusion

There are actually two ways of thinking about it

[1] Value passing, whether an array or an object is passed, is more formal because in theory the address passed by reference is also a value ~

[2] Either value passing (when passing basic data types) or reference passing (when passing address values)

I think you get the idea by now but what about practical applications? Let’s take an example

An 🌰 no. 1

var a = 3;
function fn(a){
    a = a + 1;
}
fn(a);
console.log(a);
Copy the code

What is the output here?

All asked so affirmation is pit 🀣

The answer is 3

Let me explain

var a = 3;
function fn(a){
    a = a + 1;
    // a =a +1; // a =a +1
    // The local variable is 4 and the global variable is output.
    console.log(a);/ / 4
}
fn(a);// It is not the address of a but the value of a -- 3
// Call the function to assign the argument to the parameter
console.log(a);/ / 3
Copy the code

When passing an argument, pass the value of a!

Actually, it makes a lot of sense if you understand scope

How does a = a + 1 (change value operation) occur in the function scope and my global scope 🀣

An 🌰 no. 2

function fn2(obj){
    obj.name = "change";// This modifies the name attribute of the object in the address
}
var obj = {name: "Bill"};
fn2(obj);// Call function modifies the name attribute of the object in the address
console.log(obj.name);//change
Copy the code

The argument passed in is the address value.

At this point, the value modification operation in the function scope takes effect

And you might say, well, that’s not a big deal. Why is it that you have to modify a value so broadly and then you have to modify a value locally?

Although a variable is local, the object it points to is global. I’m modifying the global object, but you’re pointing to me so the value I get from it changes

Object value changes obj. Name value naturally changes ~

summary

So much for the summary

In fact, when a function is called to pass parameters, strictly speaking, it is passing values

Some values are passed as basic data types, so you need to pay attention to the scope of the function. Some values are passed as address values, so you can change the address values regardless of the scope. It is very simple to change the object πŸ˜‚ to see the above example to understand. Der ~ but the interview should not be more ordinary words two sentences seem I think more! (indeed think a lot of feed (# ‘O’)