先定义一个构造函数
function getPoint() {
const x = 1;
const y = 10;
return {x, y};
}
接下来验证
getPoint()
//{x: 1, y: 10}
以上,说明带双括号是执行一个函数
getPoint
/* getPoint() {
const x = 1;
const y = 10;
return {x, y};
}
*/
以上,不带双括号是指对象(指针),这里返回了对象的指针,也就是这个对象
getPoint().x
//1
以上,其实是执行函数,返回了一个函数体内定义return的对象{x: 1, y: 10} ,然后取得对象的x值
getPoint.foo = 100
getPoint.foo
//100
以上,getPoint没带双括号,不是函数的执行,在这里是一个对象,对象都遵循原型链,第一句在原型上添加foo属性并赋值100
getPoint.prototype
/*constructor: ƒ getPoint()
foo: 100
length: 0
name: "getPoint"
arguments: null
caller: null
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: VM272:1
[[Scopes]]: Scopes[2]
__proto__: Object
*/
以上,调出getPoint的原型,原型上有constructor(这里就是指getPoint()构造函数)和属性foo: 100
所以getPoint()本身是函数,也是对象,看他怎么用。带双括号就是执行的意思,那这个时候他就是函数。函数本身是个对象,体现在他有Prototype,他执行的其实就是Prototype上的constructor。