编辑代码

/*
* 数组去重:只有用一次遍历:
*/

var arr = [1, 2, 1];

Array.prototype.getRepeat = function () {
    let obj = {}
    for (let i = 0; i < this.length; i++) {
        let item = this[i]
        obj[item] = obj[item] + 1 || 1
    }
    return obj
}

console.log(arr.getRepeat())


var reg = /(http|https|file|ftp:\/\/)|(^\/\/)/

console.log(reg.test('//123'))
console.log(reg.test('ftp://123'))
console.log(reg.test('https://123'))


// 实现 a、b异步函数并行执行,c函数在它们执行完才执行
// 定时器模块是由浏览器的线程管理的,所以,会能倒计时。
function a(callback) { setTimeout(callback, 2000, 'a') }
function b(callback) { setTimeout(callback, 2000, 'b') }
function c(callback) { setTimeout(callback, 30, 'c') }

function createPromise(handler) {
    return new Promise(handler)
}

// console.time('timerID: 输出为2秒')
let aa = Promise.all([createPromise(a), createPromise(b)])
    .then(res => {
        console.log(res)
        // console.timeEnd('timerID: 输出为2秒')
        return createPromise(c)
    })
    .then(res => console.log(res))

console.log(aa)

// 组合继承
function Person(name, age) {
    this.name = name;
    this.age = age;
}
Person.prototype.sayHello = function () {
    console.log('hello world')
}

function Son(name, age) {
    Person.call(this, name, age)
    this.zi = '自身独有的数据'
}
// 类似1:这样的原型继承,虽然在原型链上拼接(包装)了一层,但多出一些属性,不优美。
// Son.prototype = new Person()

// 类型2:使用 API 来创建一个干净的原型,包装
Son.prototype = Object.create(Person.prototype)

// 自定义自己独有的原型方法
Son.prototype.ziji = function () {
    console.log('自己独有的,如果父元素也有这个方法,也不至于覆盖掉原有的,因为这方法存储在干净原型上,在渗透一层才到父元素那里')
}