我有一个对象"person1“,其中有一个方法”注册“==>
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)
不允许访问注册函数中的所有方法和属性。
发布于 2021-11-08 21:55:30
为什么log
生成它所做的,而dir
产生它所做的事情,答案是它们是出于特定的原因编写的。根据文献资料
log
背后的意图是“用于日志信息的一般输出”,而dir
背后的意图是“显示指定JavaScript对象的属性的交互式列表。这个列表允许您使用公开三角形来检查子对象的内容。”
因此,log
并没有给出所有的属性、方法和其他信息。log
只是用于“一般日志记录”,以给您一些有关对象的提示。我们有dir
作为输出所有东西的明确目的。换句话说,log
没有给出详细的描述,因为控制台对象的设计人员选择将这些信息放在dir
中。
(设计人员本可以选择使用log
输出更多的内容,但他们希望为dir
保留更强大的功能。此外,您可能熟悉out控制台,点日志记录通用对象给出了通常无用的[object Object]
,因此,记录函数也没有给出完整的细节,这并不奇怪。)
https://stackoverflow.com/questions/69893352
复制相似问题