1. 下面代码输出什么
function SetCount(count) {
this.count = count
}
SetCount.prototype.printCount = function () {
console.log(this.count)
}
let a = new SetCount(100)
a.count = 200
a.__proto__.count = 300
a.__proto__.printCount()
a.printCount()
2. 请实现一个方法,可求出数组的的最大值和最小值
请实现一个方法,可求出数组的的最大值和最小值,例如:[2,3] -> 3, [4,3] -> 4
function getMaxOrMin(arr, type = 1) {
// type: 1为求最大值,0或其他不为1的数字为求最小值
if (type == 1) {
const res = arr.sort((a, b) => {
return b - a
})
return res[0]
} else {
const res = arr.sort((a, b) => {
return a - b
})
return res[0]
}
}
3. 请实现一个或多个方法,能将字符串中重复的部分去重
请实现一个或多个方法,能将字符串中重复的部分去重,例如:‘hello’ -> ‘helo’
function deduplication_one(data) {
if (typeof data !== 'string') return
let arr = data.split('')
let set = new Set(arr)
return [...set].join('')
}
function deduplication_two(data) {
if (typeof data !== 'string') return
let arr = data.split('')
let obj = {}
let result = ''
for (let i = 0; i < arr.length; i++) {
let char = arr[i]
if (!obj[char]) {
// 判断该字符是否已经存在
obj[char] = true
result += char
}
}
return result
}
4. 请用不少于2种继承方式实现,Child继承Parent
5. 请回答以下console输出结果
var name = 'Tom';
(function () {
if (typeof name == 'undefined') {
var name = 'Jack'
console.log('Goodbye ', name)
} else {
console.log('Hello ', name)
}
})()
// Goodbye Jack
// 立即执行函数可以创建一个作用域,保护私有变量不会污染全局变量,内部无法直接访问外部变量。
var a = 10;
(function (a) {
console.log(a);
a = 5;
console.log(window.a)
var a = 20;
console.log(a)
})()
// undefined
// 10
// 20
// var定义的变量可以挂载到window下
function changeObjProperty(o) {
o.siteUrl = 'https://www.example.com'
o = new Object();
o.siteUrl = 'http://example.com'
}
let webSite = new Object();
changeObjProperty(webSite)
console.log(webSite.siteUrl);
// https://www.example.com
// 在函数中,首先修改了对象 o 的 siteUrl 属性为 "https://www.example.com",
// 然后又新建了一个对象 o 并修改了其 siteUrl 属性为 "http://example.com",但这个新建的对象 o 只在函数内部有效,
// 不会影响到外部的 webSite 对象。
// 因此,最终输出的是在函数内部修改过的 webSite 对象的 siteUrl 属性,即 "https://www.example.com"。
6. 完成下列不等式
class A{}
class B extends A{}
const a = new A()
const b = new B()
a.__proto__ ===
b.__proto__ ===
B.__proto__ ===
B.prototype.__proto__ ===
b.__proto__.__proto__ ===
// 解答
console.log(a.__proto__ === A.prototype);
console.log(b.__proto__ === B.prototype);
console.log(B.__proto__ === A);
console.log(B.prototype.__proto__ === A.prototype);
console.log(b.__proto__.__proto__ === A.prototype);
7.请写出以下代码运行的结果
function Person() {}
var person1 = new Person()
var person2 = new Person()
Person.prototype.getName = function () {
return this.name
}
Person.prototype.name = 'tom'
person1.name = 'jerry'
var name = person2.getName()
console.log(name)
// tom
// 因为在Person的原型对象上定义了getName方法和name属性,
// 并将name属性的值设置为"tom"。在实例化person2时,
// 没有为其单独设置name属性,因此调用getName方法时返回的是原型对象上的name属性值"tom"。
// 而在实例化person1时,单独为其设置了name属性值为"jerry",
// 但是调用getName方法时,this指向的是person1实例,因此返回的是person1的name属性值"jerry"。
setTimeout(() => {
console.log(1)
}, 0)
new Promise(function execulor(resolve) {
console.log(2)
for (var i = 0; i < 10000; i += 1) {
i == 9999 && resolve()
}
console.log(3)
}).then(function () {
console.log(4)
})
console.log(5)
// 2 3 5 4 1
async function async1() {
console.log('async1 start')
await async2()
console.log('async1 end')
}
async function async2() {
console.log('async2')
}
console.log('script start')
setTimeout(() => {
console.log('setTimeout')
}, 0)
async1()
new Promise(function (resolve) {
console.log('promise1')
resolve()
}).then(function () {
console.log('promise2')
})
console.log('script end')
// script start, async1 start, async2, promise1, script end, async1 end, promise2, setTimeout