【发布时间】:2021-12-21 21:01:43
【问题描述】:
我有一个对象“person1”,其中有一个方法“enroll”==>
function Person(first, last, age, gender) {
this.enroll = function abc() {
console.log("hello world")
}
// property and method definitions
this.name = {
first: first,
last: last,
};
this.age = age;
this.gender = gender;
//...see link in summary above for full definition
}
let person1 = new Person('Bob', 'Smith', 32, 'male', ['music', 'skiing']);
console.log(person1);
我的问题是为什么在console.log(person1.enroll) function body or function definition(ƒ abc() {console.log("hello world")}) 上被返回,为什么整个函数对象没有像这样返回=>
ƒ abc()
arguments: null
caller: null
length: 0
name: "abc"
prototype: {constructor: ƒ}
[[FunctionLocation]]: oop2.html:12
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[2]
以及为什么我必须做console.dir(person1.enroll) 才能看到enroll 函数对象的所有属性和方法。为什么console.log(person1.enroll) 不能访问注册函数内的所有方法和属性。
【问题讨论】:
-
我认为这与this有关
标签: javascript function javascript-objects