function Person(name,age){
this.name = name
this.age = age
}
var p = new Person('张三',11)
//创建构造函数的时候,解析器会自动为构造函数创建prototype属性,prototype属性对应的对象就是原型对象
// prototype 翻译为 原型
// prototype 用于定义构造函数创建的实例对象 所共享的属性和方法
console.log(Person.prototype === p.proto) //true
// ECMAScript 标准 是 Object.getPrototypeOf()
console.log(Person.prototype === Object.getPrototypeOf(p)) //true
console.log(Person.hasOwnProperty('name')) //true
console.log(p.hasOwnProperty('name')) //true
Person.prototype.sex = '男'
console.log(Person.hasOwnProperty('sex')) //false
console.log(Person.prototype.hasOwnProperty('sex')) //true
console.log(p.hasOwnProperty('sex')) //false
console.log(p.proto.hasOwnProperty('sex')) //true
// 原型链是一种对象之间通过原型关系关联行程的链式结构
// 原型链的查找方向
// p.proto Person.prototype Object.prototype
js 原型 和 原型链
前端喜欢研究技术2023-12-16 18:52
相关推荐
lsx2024069 分钟前
CSS 网页布局:从基础到进阶蜗牛沐雨13 分钟前
警惕 Rust 字符串的性能陷阱:`chars().nth()` 的深坑与高效之道2401_858286111 小时前
125.【C语言】数据结构之归并排序递归解法独立开阀者_FwtCoder1 小时前
【Augment】 Augment技巧之 Rewrite Prompt(重写提示) 有神奇的魔法我想说一句1 小时前
事件机制与委托:从冒泡捕获到高效编程的奇妙之旅guygg881 小时前
基于matlab的FIR滤波器汤姆Tom1 小时前
JavaScript reduce()函数详解小飞悟1 小时前
你以为 React 的事件很简单?错了,它暗藏玄机!中微子2 小时前
JavaScript 事件机制:捕获、冒泡与事件委托详解双叶8362 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)