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
相关推荐
小兵张健7 小时前
开源 playwright-pool 会话池来了codingWhat11 小时前
介绍一个手势识别库——AlloyFingerLee川11 小时前
深度拆解:基于面向对象思维的“就地编辑”组件全模块解析进击的尘埃11 小时前
Web Worker 与 OffscreenCanvas:把主线程从重活里解放出来codingWhat11 小时前
手撸一个「能打」的 React Table 组件进击的尘埃11 小时前
用 TypeScript 的 infer 搓一个类型安全的深层路径访问工具yuki_uix11 小时前
Object.entries:优雅处理 Object 的瑞士军刀Lee川11 小时前
JavaScript 面向对象编程全景指南:从原始字面量到原型链的终极进化Neptune115 小时前
JavaScript回归基本功之---类型判断--typeof篇进击的尘埃16 小时前
微前端沙箱隔离:qiankun 和 wujie 到底在争什么