nodejs基本语法

变量提升: var

var tmp = new Date()
function f(){

console.log(tmp) //作用于死区
if(false){

var tmp = 'hello world'
}
}

f() tmp undefine var 变量提升

5 变量的结构赋值:
let arr = [1,2,3]
let b =arr[1]


let [a,b,c] = arr;
let [x,y,z] = [1,2] z undefine
let [x, y, z = 5] = [1,2] 设置默认值


let [m,n] = [1,2,3] m =1, n = 3;
宗旨: 一一对应
var obj ={ // 定义变量
a: 2,
b: 3

}

let obj = {
a, //=> a:a
b: b,
add(){} // =>add: function(){} //对象简写
}


let dates = { //定义对象

name: 'ss',
age: 12
}

let name = dates.name

let {name: name, age: age} = dates; //声明变量
改变量名
let {name: nm, age: ag} = dates;
let {name,age} = dates;

对象解构赋值
面向对象: 原型。 __proto__: object 原型
赋值原型属性。
let ss = [1,2,3]

let {length: len} = ss //结构赋值
len
>> 3
let datas = {
name: "aa",
age: 13,
sex: false


}
let {name,age,sex: sx=true} = datas; 修改名字

 

以数组形式的解构

let str = "abcdefg"
let [abc,io] = str; //abc =a, io = b
以字符串形式解构

let {indexof, length,slice} = str // 原型赋值


展开运算符: const arr = [1,2,3] // 展开后: ...[1,2,3] => 1,2,3
const s = [...[1,2,3]] // s=== arr false 地址不一样
var o = {...obj}
var person = {

name: 'aaa';
age: 12;
...obj // 合并对象
}

var newObj = {...obj,...person} //合并对象
function add(...arr){ // ...[1,2,3]

console.log(arr)
}

add(...arr); //展开运算符

字符串扩展: 模板字符串
const name = '小明';
const age = 12;
const str = name + age;
const newstr = `${name}今年${age}了`; //模板字符串{} 可以放任意表达式,但是一定要有返回值。
标签模板:

function add(...arr){

console.log(arr)
}

add`${name}今年${age}了${age}`; // 三个参数

类:对实例的抽象
实例:类的具体表现
原型:
const date = new Date()
console.dir();

<div id='box'> </box>
<span id='span'></span>
const oBox = document.getElementById('box')

每个标签都有自己的类。 console.dir(obj)

一切皆对象

this supper
判断的是this指向;
this 指向windows 一般只出现在函数内部。
意义让同一个函数在不同环境下执行有不同的效果。实例的方法调用的类的原型。

只有函数执行的时候才能: 判断this指向。
函数内部的this指向当前函数执行的环境。函数所挂靠的实例对象。
this指向实例对象。实例原型对象。
super调用父类的方法。
super 和this 用同样的特性。
super 指向当前函数的执行环境。所属的类原型。(property)
书写时不能单独写在函数中。
Object.property.abc = 123;
cosnt obj = {
name : "zhang",
say(){
console.log(super.abc);
console.log(this.name);

}

}


修改函数this的方法:
function 是一个实例。 通过函数推算他的类。

方法: call 参数, 返回值, this 指向, 功能。
const obj={

name: "zhang";
sayProp(){
console.log(super.abc);
console.log(this.name);
}
}
function add(a,b,c){
console.log(a,b,c);
console.log(this);

}
add.call(obj,1,2,3)
a. call 功能: 让函数执行。
apply 和call 是一样的。


bind方法: 挂载函数的类的原型上。
不能使调用bind的函数执行。
改变函数内部的this.
会生成新的函数。

箭头函数: const add = function(){}
const dis = (/*形参*/)=>{
}
简写小括号和花括号在一定情况下可以简写:
参数是一个的时候小括号可以省略;
代码块仅有一句话时花括号可以省略;

const add (a) =>{

return a+10;
}
简写: add = a =>a+10;
作为对象属性;
const obj = {
add: ()=>{}
}
箭头函数是没有this; 指的时上一级环境。

const obj={

add: ()=>{
console.log(this); //指向上一级对象
}
dis(){
const m = ()=>{
console.log(this);
}

const oo = {m}
oo.m();
}
}

const person = {
say(){
const m = ()=>{
console.log(this); //指向person

}
m();
}

}

类基本用法:
代码中有很多类。
首字母大写。
class Person{

//针对类的描述
constructor(){ 成员方法, this都指向生成实例。接受参数。实例化的参数会被constructor接受到。
this.name = '张三'; // this 指向实例。
this.age = 12;
this.height = 178;
this.weight = 100;

} //构造函数;每个类都有
say(){

const {name,age, height,weight} = this;
console.log(`我叫${name},我的年龄${age},我的身高${height},我的体重${weight}`)
}
doAction(){}
doThing(){}
}

const p1 = new Person();
实例和类。

类里面的成员的方法都挂在类的原型上。

 

posted @ 2020-03-04 23:04  countryboy666  阅读(1099)  评论(0编辑  收藏  举报