16、寄生式组合继承
<script>
function Animal(name) {
this.name = name
this.colors = ['black', 'white']
}
Animal.prototype.getName = function () {
return this.name
}
function Dog(name, age) {
Animal.call(this, name)
this.age = age
}
Dog.prototype = Object.create(Animal.prototype)
Dog.prototype.constructor = Dog
let dog1 = new Dog('奶昔', 2)
debugger
dog1.colors.push('brown')
let dog2 = new Dog('哈赤', 1)
debugger
console.log(dog2)
</script>
17、手写new
<script>
function Foo(name, age) {
this.name = name;
this.age = age;
console.log('我执行!!');
say = function () {
console.log('我也执行了!!!');
}
say();
}
Foo.prototype.sayhi = function () {
console.log(this.name);
}
const foo = new Foo('哈哈', 123);
console.log(foo);
foo.sayhi();
console.log(foo.name);
function Foo1(name) {
this.name = name;
return {
height: 180
}
}
const foo1 = new Foo('哈哈');
console.log(foo);
function myNew(fn, ...args) {
const args = Array.prototype.slice.call(arguments, 1);
const newObj = {};
newObj.__proto__ = fn.prototype;
fn.apply(newObj, args);
return newObj;
}
function _new(fn, ...args) {
const newObj = Object.create(fn.prototype);
const value = fn.apply(newObj, args);
return value instanceof Object ? value : newObj;
}
function myCreate(fn) {
function F() { }
F.prototype = fn
return new F()
}
</script>
18、数组扁平化
<script>
function myFlat1(arr) {
var result = [];
for (var i = 0, len = arr.length; i < len; i++) {
if (Array.isArray(arr[i])) {
result = result.concat(flatten(arr[i]))
} else {
result.push(arr[i])
}
}
return result;
}
function myFlat2(arr) {
while (arr.some(item => Array.isArray(item))) {
console.log(arr)
arr = [].concat(...arr);
}
return arr;
}
const arr = [1, [2, [3]]];
myFlat2(arr)
</script>
19、浅拷贝
<script>
function myShallowCopy(obj) {
if (typeof obj !== 'object') return;
let newObj = obj instanceof Array ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = obj[key]
}
}
return newObj
}
const obj = {
name: 'wujunjie',
age: 18,
say: {
name: 'junjie'
},
arr: [1, 2]
}
const tempObj = myShallowCopy(obj)
tempObj.say.newAge = 19;
tempObj.age = 20;
tempObj.name = 'xiaorong';
tempObj.arr.push(3);
console.log(obj)
</script>
20、深拷贝
<script>
const isObject = (target) => (typeof target === "object" || typeof target === "function") && target !== null;
function myDeepClone(target, map = new WeakMap()) {
if (map.get(target)) {
return target;
}
let constructor = target.constructor;
if (/^(RegExp|Date)$/i.test(constructor.name)) {
return new constructor(target);
}
if (isObject(target)) {
map.set(target, true);
const cloneTarget = Array.isArray(target) ? [] : {};
for (let prop in target) {
if (target.hasOwnProperty(prop)) {
cloneTarget[prop] = myDeepClone(target[prop], map);
}
}
return cloneTarget;
} else {
return target;
}
}
const obj = {
name: 'wujunjie',
like: {
name: 'yuanxiaorong'
},
date: new Date()
}
const newObj = myDeepClone(obj);
newObj.like.age = 18;
const arr1 = []; console.log(arr1.constructor.name)
const arr2 = {}; console.log(arr2.constructor.name)
const arr3 = function () { }; console.log(arr3.constructor.name)
const arr4 = new Date(); console.log(arr4.constructor.name)
const arr5 = new RegExp(); console.log(arr5.constructor.name)
const arr6 = function () { }; const newArr = new arr6(); console.log(newArr.constructor.name)
const arr7 = 7; console.log(arr7.constructor.name)
const arr8= 'nice'; console.log(arr8.constructor.name)
</script>