注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
都是在牛客在线面试。
一面 代码分析+实现
1. 分析代码 (变量的定义).1point3acres
- (function () {
- const a = b = 2;
- })()
- console.log("a=", a, " b=", b);
复制代码 .
2. 分析代码 (this,要多了解JS的作用域和执行环境!)
. Χ
- function hello() {
- setTimeout(function() {
- console.log(this.userName);
- }, 0);
- }
- hello.call({ userName: 'Tom' });
复制代码
Follow up: 如果想正确输出'Tom'要怎么修改
-baidu 1point3acres
3. 分析代码 (隐式类型转换规则)
- if ([] == false) console.log(1);
- if ({} == false) console.log(2);
- if ([]) console.log(3);
- if ({}) console.log(4);. 1point3acres.com
- if ([1] == [1]) console.log(5);
复制代码 .--
// 凉透了,以下是看了学习资料得出的各种推算过程。。
https://blog.csdn.net/lantian_bupt/article/details/107566580
4. 分析代码 (前端面试必学!学会宏任务和微任务,这类变体都不怕!).1point3acres
.1point3acres
- function newPromise = () => Promise.resolve("I have resolved");
- function first() {
- newPromise().then(res => {
- console.log(res + " first");.
- })
- console.log("first");. Χ
- }
- async function second() {
- console.log(await newPromise());
- console.log("second");. From 1point 3acres bbs
- }
- first();
- second();
复制代码
5. 分析代码 (声明提前).google и
- (function() {
- console.log("a=", a);. check 1point3acres for more.
- console.log("b=", b);
- var a = 2;
- let b = 3;
- })()
复制代码
lz自己加的扩展:函数体提前发生在变量声明提前之前
- console.log(a)//?
- a();//?
- var a =3;
- function a(){
- console.log(10);
- }
- console.log(a);//?
- a = 6;. From 1point 3acres bbs
- a();//?
复制代码
6. 分析代码
- const getKey = (function(a) {
- this.a = a;
- return function (b) {. ----
- return this.a + b;
- }
- } (function(a, b, c) {
- return ~~a + ~~b + ~~c;
- }.call(1, 2, 2)))
- const getArray = function(arr) {-baidu 1point3acres
- let res = [];
- for (let i=0; i<arr.length; ++i) {
- res[i] = function() {.
- return arr[i];
- }
- }
- return res;
- }
- const arr = getArray([10, 20, 30, 40, 50, 60, 70, 80, 90]);
- console.log(arr[getKey(1)]());
复制代码
// 面试官一开始就先提醒:两个波浪线会将undefined/null等转成数字0,原本是数字的还是保留原来的数字。.1point3acres
. check 1point3acres for more.
7. 实现Lazy Man. Waral dи,
写个Man继承自Person,并且支持如下调用:
- function Person(name, age) {
- this.name = name;
- this.age = age;.1point3acres
- }
- function Man() {} //这部分需要实现
- Man('Tom').sleep(3).eat('dinner');
复制代码 . .и
期望输出:
Hello! I'am Tom, 20 years old.
Wait up in 3 seconds.
Eat dinner.
.google и
面试官都很nice,不会的题会很耐心地引导,最不济也会引导猜一下结果,不过lz基础太差还是凉了。
. .и
最后祝各位帅哥美女牛年大吉,offer冲天! 可以的话赏点大米!
|