Javascript 新关键字
Javascript new keyword
以下方式创建实例是什么意思?
somevar = new Person.doSomething()
这是创建实例和调用方法的快捷方式吗,比如
person = new Person ()
person.doSomething()
还是其他原因?
提前致谢。
不,这不会创建 Person
的新实例,然后对其调用方法。它创建一个 Person.doSomething()
是什么的实例。所以,实际上,你这两个是等价的:
const Person = {
doSomething: function(foo, bar){
this.foo = foo;
this.bar = bar;
}
}
//create a new instance of `Person.doSomething`
const p1 = new Person.doSomething(1, "one");
//take a reference of `Person.doSomething`
const temp = Person.doSomething;
//create an instance of it
const p2 = new temp(2, "two");
console.log("p1:", p1);
console.log("p1 instanceof Person.doSomething:", p1 instanceof Person.doSomething);
console.log("p2:", p2);
console.log("p2 instanceof Person.doSomething:", p2 instanceof Person.doSomething);
您只能使用 constructable 函数获取实例。这些是普通函数(使用 function
关键字声明)和 class
构造函数:
function PlainConstructable(a, b) {
this.foo = a;
this.bar = b;
}
const plainInstance = new PlainConstructable("plain", "instance");
class ClassConstructable {
constructor(a, b) {
this.foo = a;
this.bar = b;
}
}
const classInstance = new ClassConstructable("class", "instance");
console.log(`plainInstance:
instanceof PlainConstructable: ${plainInstance instanceof PlainConstructable}
what it holds: ${JSON.stringify(plainInstance)}`);
console.log(`classInstance:
instanceof ClassConstructable: ${classInstance instanceof ClassConstructable}
what it holds: ${JSON.stringify(classInstance)}`);
不可建造的有:
- arrow functions
- shorthand methods 在对象中或 类
- 大多数内置函数(例外情况在文档中明确提及)
const arrowFunction = () => {};
const plainObject = {
shorthandMethod() {}
}
try {
new arrowFunction();
console.log("Constructing arrow function successful.");
} catch(e) {
console.log(
`Cannot construct arrow function
${e}`
)
}
try {
new plainObject.shorthandMethod();
console.log("Constructing shorthand method successful.");
} catch(e) {
console.log(
`Cannot construct shorthand method
${e}`
)
}
try {
new parseInt();
console.log("Constructing built-in successful.");
} catch(e) {
console.log(
`Cannot construct built-in
${e}`
)
}
以下方式创建实例是什么意思?
somevar = new Person.doSomething()
这是创建实例和调用方法的快捷方式吗,比如
person = new Person ()
person.doSomething()
还是其他原因?
提前致谢。
不,这不会创建 Person
的新实例,然后对其调用方法。它创建一个 Person.doSomething()
是什么的实例。所以,实际上,你这两个是等价的:
const Person = {
doSomething: function(foo, bar){
this.foo = foo;
this.bar = bar;
}
}
//create a new instance of `Person.doSomething`
const p1 = new Person.doSomething(1, "one");
//take a reference of `Person.doSomething`
const temp = Person.doSomething;
//create an instance of it
const p2 = new temp(2, "two");
console.log("p1:", p1);
console.log("p1 instanceof Person.doSomething:", p1 instanceof Person.doSomething);
console.log("p2:", p2);
console.log("p2 instanceof Person.doSomething:", p2 instanceof Person.doSomething);
您只能使用 constructable 函数获取实例。这些是普通函数(使用 function
关键字声明)和 class
构造函数:
function PlainConstructable(a, b) {
this.foo = a;
this.bar = b;
}
const plainInstance = new PlainConstructable("plain", "instance");
class ClassConstructable {
constructor(a, b) {
this.foo = a;
this.bar = b;
}
}
const classInstance = new ClassConstructable("class", "instance");
console.log(`plainInstance:
instanceof PlainConstructable: ${plainInstance instanceof PlainConstructable}
what it holds: ${JSON.stringify(plainInstance)}`);
console.log(`classInstance:
instanceof ClassConstructable: ${classInstance instanceof ClassConstructable}
what it holds: ${JSON.stringify(classInstance)}`);
不可建造的有:
- arrow functions
- shorthand methods 在对象中或 类
- 大多数内置函数(例外情况在文档中明确提及)
const arrowFunction = () => {};
const plainObject = {
shorthandMethod() {}
}
try {
new arrowFunction();
console.log("Constructing arrow function successful.");
} catch(e) {
console.log(
`Cannot construct arrow function
${e}`
)
}
try {
new plainObject.shorthandMethod();
console.log("Constructing shorthand method successful.");
} catch(e) {
console.log(
`Cannot construct shorthand method
${e}`
)
}
try {
new parseInt();
console.log("Constructing built-in successful.");
} catch(e) {
console.log(
`Cannot construct built-in
${e}`
)
}