对象
let obj = { name: 'test', age: 12 }
let obj1 = { name: 'test1', sex: 1 }
let obj2 = Object.assign(obj, obj1)
console.log('Object.assign')
console.log(obj2)
let obj3 = { ...obj, ...obj1 }
console.log('{ ...obj, ...obj1 }')
console.log(obj3)
Object.prototype.merge = function(obj) {
console.log(this)
for (let o in obj) {
this[o] = obj[o]
}
return this
}
let obj4 = obj.merge(obj1)
console.log('obj.merge')
console.log(obj4)
//super 可以调用父类的方法
let parentObj = {
name: 'p',
getName: function() {
return this.name
}
}
console.log('super')
let childObj = {
__proto__: parentObj,
getName() {
console.log(super.getName())
}
}
childObj.getName()
函数
/*默认参数*/
function ajax(
url = new Error('url不能为空'),
method = 'get',
dataType = 'JSON'
) {
console.log(url)
console.log(method)
console.log(dataType)
}
ajax('/user')
/*
求和 reduce 从左往右
*/
let list = [1, 2, 3, 4, 5]
let sum = list.reduce(function(item, val, index, origin) {
//item 和 val 这次数 index 下标 origin 数组列表
console.log(item, val, index, origin)
return item + val
}, 0)
console.log(sum)
/*
求和 reduceRight 从右往左
*/
let sum1 = list.reduceRight(function(item, val, index, origin) {
//item 和 val 这次数 index 下标 origin 数组列表
console.log(item, val, index, origin)
return item + val
}, 0)
console.log(sum)
//reduce 实现原理
Array.prototype.reduce1 = function(reducer, initialVal) {
for (let i = 0; i < this.length; i++) {
initialVal = reducer(this[i], initialVal)
}
return initialVal
}
let sum2 = list.reduce1(function(item, val) {
return item + val
}, 0)
console.log(sum2)
//展开运算符
let arr = [1, 2, 3]
let arr1 = [4, 5, 6]
let arr2 = [...arr, ...arr1]
console.log(arr2)
//合并对象
//1 for in 2 Object.assign 3.结构赋值
let obj = {
name: '1'
}
let obj1 = {
age: 23
}
let obj2 = {}
for (let key in obj) {
obj2[key] = obj[key]
}
for (let key in obj1) {
obj2[key] = obj1[key]
}
console.log(obj2)
let obj3 = {}
Object.assign(obj3, obj, obj1)
console.log(obj3)
let obj4 = { ...obj, ...obj1 }
console.log(obj4)
集合
// set map
let set1 = new Set()
set1.add(12)
console.log(set1)
箭头函数
//箭头函数没有自己的this 它会使用上层的this 在上层方法体里面
let name = 'window'
let obj = {
name: '123',
getName: () => {
console.log(this)
console.log(this.name)
}
}
obj.getName()
let a = () => {
console.log(this)
}
a()
let obj1 = {
name: '123',
getName: function() {
console.log(this)
console.log(this.name)
}
}
obj1.getName()
let obj2 = {
name: '234',
fun: function() {
console.log(this)
return () => {
console.log(this)
}
}
}
console.log('obj2')
obj2.fun()()
类
class Parent {
constructor() {
this.name = '123123' //私有属性
}
//静态属性是类的属性 通过类直接调用
static hello() {
console.log('hello')
}
//公共属性 相当于原型上的属性
getName() {
console.log(this.name)
}
}
new Parent().getName()
let p = new Parent()
Parent.hello()
class Child extends Parent {
constructor(name, age) {
//super 指的是父类的构造函数
super(name)
this.age = age
}
getAge() {
console.log(this.age)
}
}
生成器、迭代器
/**
*
* 生成器(Genetator) 迭代器(Iterator)
*/
//read 生成器 用来生成迭代器的
function read(books) {
let index = 0
return {
next() {
let done = index == books.length - 1
console.log(index)
let value = books[index++]
console.log(index)
return {
done,
value
}
}
}
}
//迭代器可以不停的调用next方法得到一个结果,{value,done}
//当done为ture的时候表示去完了
let it = read(['js', 'node'])
//it 有一个方法叫next,每次调用都会返回一个结果{value,done}
// let r1 = it.next()
// let r2 = it.next()
// console.log(r1)
// console.log(r2)
let result
do {
result = it.next()
console.log(result)
} while (!result.done)
//生成器函数和普通函数长得不一样,返回迭代器
//执行的时候也不一样
console.log('生成器----------')
function* read1(books) {
for (let i = 0; i < books.length; i++) {
// yield 放弃 屈服的意思
yield books[i]
}
}
let it1 = read1(['js', 'node'])
let r1 = it1.next()
let r2 = it1.next()
let r3 = it1.next()
console.log(r1)
console.log(r2)
console.log(r3)
字符模板
/*
* var
* 1.
* */
var name = 123;
function replace(desc) {
return desc.replace(/\$\{([^}]+)\}/g, function (matched, key) {
console.log(arguments)
debugger;
return eval(key);
})
}
console.log(replace("${name}asdfasdf"));
let list = [{id: 1, name: "test"}, {id: 2, name: "test1"}];
/*改变老数组 生成新数组*/
let newList = list.map(function (item, index) {
console.log(`${item}---${index}`)
return 123
});
console.log(newList);
/*其他运算符 会吧后面所以的参数都放在一个数组里*/
function desc(string, ...rest) {
let result = '';
for (let i = 0; i < string.length-1; i++) {
result += (string[i] + rest[i])
}
console.log(string);
console.log(rest);
result += string[string.length - 1];
return result;
}
let username = "asdfasdf";
let str = desc`阿斯顿发${username}几岁了${username}`;
console.log(str);
/*startWith 以什么开头 返回布尔值 例子 http 同意 ftp*/
/*endWith 以什么结尾 返回布尔值 例子 a.png b.jpg*/
/*includes 是否包含*/
/*repeat 包含了多少次*/
// padStart(2,0); 前面补齐两位 补齐部分为0
// padEnd(2,0);后面补齐两位 补齐部分为0
let str1 = "7";
let str1Start = str1.padStart(2,0);/*07*/
let str1End = str1.padEnd(2,0);/*70*/
console.log(str1Start,str1End);