JavaScript summarizing: data types, data & variable & memory, object, function

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44627407/article/details/102666958

Data type: 1 to 6

1. What classification?
Basic (value) Type : --5 Species
string: any string
number: arbitrary numbers, positive and negative, regardless of the integer and fractional
Boolean: to true, to false
null: null
undefined: undefined

**对象(引用)类型**:
Object:任意对象都是object
Function:一种特别的对象(可以执行,即函数时可以调用的对象)
Array:一种特殊的对象(有数值下标、内部数据是有序的)

2. Analyzing:
tyoeof : Returns the string representation of the basic types, i.e., returns a string, for example: typeof a
based: string, number, boolean, undefined , is determined not null
the instanceof : determining the particular type of object (because all the objects are object type, it is necessary to determine what further specific type), Example: A instanceof array
based array and function, and can not be determined null object
=== : Analyzing be undefined, null, value not determined, because only this null and undefined there are two types of unique values
Q: determining an array method:
. 1) the instanceof the array a
2) the array the isArray (a)
method 3) of the prototype chain:? ? ?
Define an array: var a = []
and Comparative prototype comparison: Array.prototype.isPrototypeOf (a)
chestnut:

 	  var a
	  var b = function () {
	  }
	  var c = null
	  console.log(typeof a)  // undefined
	  console.log(typeof b)  // function
	  console.log(typeof c)  // objact
	  console.log(null === c) // true
	  console.log(undefined === a) // true

Difference 3.null and undefined - they do not have anything on behalf of
undefined: defined but not assigned
null: the definition and assignment, but the initial value null
null is primitive, but it tyoeof null return 'object', this contradiction? No contradiction examples:
// initially:
var = null // B show the object to be assigned, for now set to null, then it is possible to:
// object is determined assignment
b = [1,2,3], or b = { name: 'KK', Age: 18 is}
// Finally
b = null // to point b so that the object becomes garbage objects (to be garbage collected - browser)

4. When will assign values to variables null?
1) indicates that the variable will be assigned to an object behind
2) null before the end of the assignment, so that objects become garbage objects (the garbage collected)

5. strict distinction between variable type and data type?
Data Type: The
base type
object type
type (type variable memory values) of variables:
basic types: the type of the saved data is the basic
reference types: address value is stored

6. What is an example? What type?
Examples are referred to instances of an object, the object type is referred to as the type of
function Person (name, age) { // constructor type
this.name name =
this.age = Age
}
var P = the Person new new ( 'Lily', 18 is) / / the instance of an object type is created when the function is used to create an instance constructor is
// Person ( 'lily', 18 ) can be so called, but this time is not a constructor Person

Data, arguments, memory 7 to 10

7. What is the data?
What information is stored in memory on behalf of a memory, essentially ... 010101
, for example, var age = 18
characteristic data:
can be transferred, such as var a = 3; var b = a
can operations such var b = a + 2

8. What is memory?
After the memory energized, the space can store data (temporary and permanent storage is a hard disk space), the virtual invisible.
Generating and death memory: memory (circuit board) ==> energization ==> memory space generated ==> == storing data> data processing ==> Data memory space and disappearance
classification memory:
stack memory: global, local, variable
heap memory: an object (a variable representing the object in the stack memory)

9. What is a variable
amount varying with variable names and values of the composition (the variable name memory identifier)
each variable corresponds to a small memory, find the corresponding variable names to memory, for example. 1:
var = 18 is A
Console .log (a) // subjected procedure: find variable names corresponding to a a memory, the memory storing data and then removed, and then print out
Example 2:
var obj = {name: 'Kally'}
var obj = a / / obj to copy the saved address to a variable a
console.log (obj.name) // first find this memory obj, then obj memory stores the address of an object, this address holds the value of the object

变量的读和写:
a = {sayHello: 'hello' } -- 写
其他情况均为读,如,a.name 、 a() 、a[]...都是读

10. The data, variables, the relationship between the memory of the three?
The memory space for storing data, the memory variable identification

Q:var a = xxx,a内存中保存的是什么?
A:xxx是基本类型数据时,a内存保存的是这个数据
  xxx是引用类型数据时,a内存保存的是对象的地址
  xxx是一个变量时,a内存保存的有可能是这个数据,可能是地址值

Q:关于变量赋值的问题:
A1:两个引用变量指向同一个对象时,通过一个变量改变对象内部的数据,另一个变量的内部数据也会发生改变
var obj1 = {
	  name: 'lily'
	}
	function fn1(obj){
	  obj.name = 'kally'
	}
	fn1(obj1)
	console.log(obj1.name)  // kally
	function fn2(obj) {  // 形参的本质是变量、实参的本质是数据
	  var obj2=obj // 此时obj2和obj指向同一个对象
	  obj.name='Bob'
	}
	fn2(obj1)
	console.log(obj1.name) // Bob
A2:两个引用变量指向同一个对象时,让其中一个对象指向另一个对象另一引用变量仍然指向前一个对象
var a ={age:12,name:'kaka'}
	var b = a
	a= {name:'hana',age:18} // a已经指向另一一个对象(另一块内存)-- 此时a指向另一个对象,和b已经不是指向同一个对象
	b.age=20 // b仍然指向之前的一个对象
	console.log(b.name,b.age,a.name,a.age) // 'kaka',20 "hana" 18
Q:在js调用函数时传递变量参数,是值传递还是引用传递?--两种解释,说清楚就行
理解1:值传递!只是引用型数据的值是地址!基本类型就是数据本身!
理解2:可能是值传递,也可能是引用传递(地址值)
var a=3
	function fn(a){
	  a= a+3 // 传进来的不是a而是3
	}
	fn(a)
	console.log(a) // 3
Q:JS引擎如何管理内存?
1.生命周期:
  分配小内存空间,得到使用权
  储存数据,可以反复进行操作
  释放内存空间

2.释放内存
  局部变量,函数执行结束后自动释放
  对象:成为垃圾对象(置为null)==>垃圾回收器回收
  全局变量:运行过程中都不会被释放内存
function fn(){
	var b ={}
	}
	fn() // b是自动释放内存,b所指向的对象会在后面的某个瞬间被垃圾回收器回收
	

Object: 11 to 14

11. What is the object?
The object is to save more data containers of
a thing a representative of real-world objects

12. Why objects?
To facilitate unified management of multiple data, a data does not need to declare a

13. The composition of the object? - Method properties and
attributes - the name attribute (the string), and an attribute value (of any type) consisting of
a method - a particular attribute (function value)

14. how to access internal data objects?
The object attribute name: attribute name can not contain spaces, '-' special characters, not all cases can use
the object [ 'property name']: attribute name can contain spaces, '-' and other special characters, general, any situation can be used

Q: When must use: Object [ 'attribute name']?
A: 1) attribute name contains a space, '-' when other special character
2) is a digital attribute name
var} P = {
P [23 is] = 'HH'
the console.log (P [23 is])
. 3) uncertain variable name when, for example, the attribute name stored in a variable example:

P = {} var
var proname = 'myAge'
var value = 18 is
// // p.proName attribute name = value stored in the variable
console.log§ // // given
p [proName] = value // save the attribute name in variable
console.log§ myAge // {:} 18 is
the console.log (P [proname]) // 18 is

Function: 15 to 22

15. What is the function?
N may be implemented in a feature article package statement
only functions may be performed, not perform other types of data

16. Why use functions? - function is reflected in the package of ideas, you write a function to let other people be able to understand
improve code reuse
17. how to define a function? - function declaration, a function expression
function declaration: function fn () {// operations} - can function to enhance the
function expressions: var fn = function () { // operations} - not lift function

18. How to call (execution) function?
1) direct call: fn ()
2) as a constructor is called: new new fn ()
. 3) temporary call: fn.call (test) /fn.apply (test ) - Temporary fn to call the function test

 var obj = {}
	  function fn(){
	    this.name='hello world!'
	  }
	  fn.call(obj ) // 相当于obj.fn(),call\apply提供了obj临时去访问不属于它自身的函数
	  console.log(obj.name) // hello world!

19. callback
1) What is the callback function?
Meet three characteristics:
you define, you do not have to call, finally executed (under certain conditions, such as the timer reaches a point in time, another example, click the button trigger)
2) common callback function
* dom event callback function, such as both dom click event element
* timer callback function, such as the setTimeout
* ajax request a callback function - back summary
* lifecycle callbacks - summary behind
examples:

document.getElementById('btn').onclick = function (){  // dom事件回调函数
	  alert('点击事件回调函数')
	}
	// 定时器
	// 超时定时器
	setTimeout(function () { // 定时器回调函数
	  alert('hello world')
	},3000)
	
	// 循环定时器
	setInterval(function () { // 定时器回调函数
	  alert('hello piixi')
	},3000)
Q:什么叫交互?
用户交互:比如用户点击要有什么反应?你得绑定某个事件来相应用户的操作 --与人的交互
前后台交互:前台数据从后台动态获取,前台需要写请求函数向后台获取数据等

20.IIFE-Immediately Invoked Function Expression, immediately call the function expression is usually said an anonymous call from a function
examples:

(function (){  // 匿名函数自调用
console.log('hello world')
})()  // hello world

Effect (advantage):
1) hide implementation - the implementation is not exposed to the outside, make the inside anonymous function
2) does not contaminate the external (global) namespace
3) with which the encoding module js
Example 1:

(function (){  // 匿名函数自调用
                 var a =3  // 局部属性,不会污染匿名函数外部(全局)命名空间
                 console.log(5+a)
                 })()  // 8

Example 2:

(function () {
      var a = 1
      function test() {
        console.log(++a)
      }
      window.$=function () {  // 向外暴露一个全局函数
        return {
          test: test
        }
      }
    })()
    $().test()  // 2 ,$是一个函数,$执行后返回的是一个对象,对象的值是test函数

This 22. Function
Q1: What is this that?
All internal functions have this variable, pointing to its caller - that is, the function must be called by an object that is this function; if not specified, this is the window

Q2:如何确定this的值?
test(); --window
p.test(); --p
(隐式指向)new test(); --新创建的对象
test.call/apply(obj) -- obj
栗子:
function Person(color) {
	    console.log(this) // 会被执行
	    this.color = color;
	    this.getColor = function () {
	      console.log(this) // 没有调用相关属性,不会被执行
	      return this.color;
	    };
	    this.setColor = function (color) {
	      console.log(this) // 没有调用相关属性,不会被执行
	      this.color = color;
	    };
	  }
	  Person("red"); //this是谁? --window,因为Person是全局函数,调用Person是window对象(Person函数调用时仅有第一句concole.log被执行)
 var p = new Person("yello");  // this是谁?--p,因为此时p指向new出来的这个对象(也是仅有第一句concole.log被执行)
	  p.getColor();  // this是谁? --p
	  var obj = {};
	  p.setColor.call(obj, "black"); //this是谁? --obj
	  var test = p.setColor;
	  test();  // this是谁? --window
 function fun1() {
	    function fun2() {
	      console.log(this);
	    }
	    fun2(); //this是谁?--window,fn2只是在fn1内部,但是fn1并没有调用fn2,即没有指定,所以此时this是window
	  }
	  fun1();
**“题外话”**--js代码应不应该加分号?
看个人喜好加不加分号,但是有**两种情况必须加分号**:
1.小括号(比如,匿名自运行函数)前必须加分号,否则在代码合并压缩(合并时)出现问题---解决方式:在行首加分号
  栗子:
  var a = 'hello'
	  //(function () {
	  //  console.log('hello world')
	  //})()  // 会报错
	
	  ;(function () {  // 匿名自运行函数前加分号
	    console.log('hello world')
	  })()  // hello world
2.中方括号前必须加分号
  ;[1,2,3].forEach(function (item,index) {
})

Guess you like

Origin blog.csdn.net/weixin_44627407/article/details/102666958