Summary of background

Hello, I’m MeiLiAo pig, I belong to sprout new front one, this is the first time writing articles on the nuggets, writing is not good, please take care of a lot, last week have received bytes to beat the interview invitation, or happy, or that sentence, the most important foundation, although I have hung up the second interview, but I still want to share about basis to some of the more interesting subject

The title

Still more easily in the first round of the interview, the interviewer said from the start side is relatively simple, let me take it easy, very nice, most interview should start chat technology (if chat is not very good, it is estimated that also can’t get the live below write code), then the bytes after beating is the live cattle from writing code, The interviewer had written two lines of code

let a = new Foo() //a.id -> 1
let b = new Foo() //b.id -> 2
Copy the code

The solution process

Using global variables

As a cute new me, see this, so easy, write ~

let idIndex = 1;
function Foo () {
  this.id = idIndex++;
}
let a = new Foo();
console.log(a);
let b = new Foo();
console.log(b);
Copy the code

Obviously achieved the effect, for the sprout new is also particularly good to understand, each instantiation of the object, the global attributes after the addition, perfect to meet the requirements of the interviewer, I saw the interviewer leisurely said, well can, but can not use global variables…

Using static properties

What’s so hard about that? Sayin! Just change global to static

function Foo () {
  this.id = Foo.idIndex++;
}
Foo.idIndex = 1
let a = new Foo();
console.log(a);
let b = new Foo();
console.log(b);
Copy the code

The interviewer scowled, he still didn’t write the code I was looking for, and he said, “Well, can I do this without using global and static properties?” And all I could think of was closure, and I said, “What about closure?” “The interviewer says you should show it

Using closures

This is still very simple for me, I write code is a shuttle

const Foo = (function(){
  let idIndex = 1;
  return function(){
    this.id = idIndex++;
  }
})()
let a = new Foo();
console.log(a);
let b = new Foo();
console.log(b);
Copy the code

Shua shua shua several times to finish writing, the interviewer nodded, HMM, it seems to be more satisfied with my writing, “ok, then we deepen the difficulty of the topic, the original topic changed to this

let a = Foo() //a.id -> 1
let b = new Foo() //b.id -> 2
let c = new Foo() //c.id -> 3
let d = Foo() //d.id -> 4
Copy the code

Use instance of rescue

At the beginning, I was stuck for a while because I had to deal with this problem, but the interviewer still gave me a friendly prompt about how to determine whether a method should be called directly or called by new, and whether instanceof was used. Fortunately, I was quick to write the code. Of course, there is such a section of code debugging

const Foo = (function(){
  let idIndex = 1;
  return function(){
    // this.id = idIndex++;
    console.log(this instanceof Foo)
  }
})()
let a = new Foo();
// console.log(a);
let b = Foo();
// console.log(b);
let c = Foo();
// console.log(c);
let d = new Foo();
// console.log(d);
Copy the code

The code above is pretty obvious, and it’s going to print true, false, false, true, because the first and the last one uses new, which is an instance of Foo, and then you’re going to make a judgment based on that, and first of all you’re going to have to have some understanding of new, because new is going to change this to, If you call a function directly without using new, the function returns undefined by default if it does not return a value, you can do this problem knowing that

const Foo = (function(){
  let idIndex = 1;
  return function(){
    // console.log(this instanceof Foo)
    if (this instanceof Foo) {
      // Using new automatically returns the object ~
      this.id = idIndex++;
    } else {
      // Instead of using new, we help it return ~
      return {
        id: idIndex++
      }
    }
  }
})()
let a = new Foo();
console.log(a); // {id: 1}
let b = Foo();
console.log(b); // {id: 2}
let c = Foo();
console.log(c); // {id: 3}
let d = new Foo();
console.log(d); // {id: 4}
Copy the code

This completes the first pen test ~

conclusion

In general, the first topic is more relaxed and pleasant, behind the title of the subsequent I will also share slowly, in fact, a still more basic questions most, the most difficult may be let you write a Promise, and to realize the Promise of some static methods, one side of the interviewer or satisfactory to me, so also increased the confidence, Then was the second round of interview blood abuse, here is the MOE new front-end development, record their own interview experience, and spare time to share some knowledge, I hope to grow together with you, if there is a mistake, I hope the big guy light spray, and can guide the way ~ thank you for your support ~