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
相关推荐
To_OC1 小时前
LC 49 字母异位词分组:想到哈希表很简单,选对 key 才是精髓kyriewen4 小时前
用了半年 Claude Code 后,我尝试关掉它写了一周代码——结果比想象中严重山河木马5 小时前
矩阵专题0-webGL中的矩阵Asize6 小时前
多模态生图:从 Vite 工程化到前端调用 Qwen Image陳陈陳6 小时前
从Token到Embedding:一篇文章搞懂大模型的「文字数学变形记」用户938515635076 小时前
从 O(n²) 到 O(nlogn):一文读懂快速排序的“快”与“妙”橘子星6 小时前
LLM 无状态架构实践:从原理到代码落地To_OC7 小时前
手写快排次次翻车?别死背快排模板了,这才是面试官想听的底层逻辑风止何安啊8 小时前
网课倍速痛点解决:一套前端代码实现自由控速播放器光影少年9 小时前
原生DOM操作在React 中的注意事项