- JavaScript 函数是引用类型 (对象类型) 所以 Function 函数也是对象
- "Function 构造函数" 也是一个对象,所以也有
__proto__
属性- "Function 构造函数" 的
__proto__
属性指向 "Function 原型对象"
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person(name, age) {
this.name = name;
this.age = age;
}
let obj = new Person("BNTang", 28);
console.log(Function.__proto__);
console.log(Function.__proto__ === Function.prototype);
</script>
</head>
<body>
</body>
</html>
- JavaScript 中还有一个系统提供的构造函数叫做
Object
- 只要是函数都是 "Function 构造函数" 的实例对象
- 只要是对象就有
__proto__
属性,所以 "Object 构造函数" 也有__proto__
属性- "Object 构造函数" 的
__proto__
属性指向创建它那个构造函数的 "原型对象"
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person(name, age) {
this.name = name;
this.age = age;
}
let obj = new Person("BNTang", 28);
console.log(Object);
console.log(Object.__proto__);
console.log(Object.__proto__ === Function.prototype);
</script>
</head>
<body>
</body>
</html>
- 只要是构造函数都有一个默认的属性,叫做
prototype
-
prototype
属性保存着一个对象,这个对象我们称之为 "原型对象"
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person(name, age) {
this.name = name;
this.age = age;
}
let obj = new Person("BNTang", 28);
console.log(Object.prototype);
</script>
</head>
<body>
</body>
</html>
- 只要是原型对象都有一个默认的属性,叫做
constructor
-
constructor
指向当前原型对象对应的那个 "构造函数"
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person(name, age) {
this.name = name;
this.age = age;
}
let obj = new Person("BNTang", 28);
console.log(Object.prototype.constructor);
console.log(Object.prototype.constructor === Object);
</script>
</head>
<body>
</body>
</html>
- object 函数的注意点,object 的
__proto__
是指向null
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person(name, age) {
this.name = name;
this.age = age;
}
let obj = new Person("BNTang", 28);
console.log(Object.prototype.__proto__);
</script>
</head>
<body>
</body>
</html>