markdown JS:'新'关键字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown JS:'新'关键字相关的知识,希望对你有一定的参考价值。

## 'new' keyword

The new keyword invokes a function in a special way. Functions invoked using the new keyword are called constructor functions.

So what does the new keyword actually do?

1. Creates a new object.
2. Sets the object’s prototype to be the prototype of the constructor function.
3. Executes the constructor function with this as the newly created object.
4. Returns the created object. If the constructor returns an object, this object is returned.

```sh
// In order to better understand what happens under the hood, lets build the new keyword 
function myNew(constructor, ...arguments) {
  var obj = {}
  Object.setPrototypeOf(obj, constructor.prototype);
  return constructor.apply(obj, arguments) || obj
}
```

What is the difference between invoking a function with the new keyword and without it?

```sh
function Bird() {
  this.wings = 2;
}

/* invoking as a normal function */
let fakeBird = Bird();
console.log(fakeBird);    // undefined

/* invoking as a constructor function */
let realBird= new Bird();
console.log(realBird)     // { wings: 2 }
```

以上是关于markdown JS:'新'关键字的主要内容,如果未能解决你的问题,请参考以下文章

nuxt.js中使用markdown编辑器

Response.Redirect打开新页面

markdown 超链接打开新窗口

SQL如何根据一个字段的某个关键词的前面部分分组查询

在JS中,new关键字做了什么

在JS中,new关键字做了什么