js 原型原型链
2023-11-20 20:04:52
个人的理解,有什么不正确的请指教,共同学习
//声明一个构造函数Person
function Person(name,age){
this.name = name;
this.age = age;
}
//向构造函数的原型添加方法getName
Person.prototype.getName= function () {
console.log(this.name);
};
//new一个对象stru1 stru1就拥有getName方法
*所有通过构造函数new出来的对象都拥有getName方法
var stu1=new Person("xiaoli","25"); console.log(stu1.name);
console.log(stu1.age);
stu1.getName();
//为什么都拥有getName方法呢
//因为__proto__,js在创建对象(不论是普通对象还是函数对象)的时候,都有一个叫做__proto__的内置属性,
// 用于指向创建它的函数对象的原型对象prototype。
console.log(Person.prototype);//打印如下:
//Person.prototype里面含有 getName方法 constructor 和__proto__
console.log(stu1.__proto__===Person.prototype); //true
console.log(Person.prototype.__proto__===Object.prototype);//true
console.log(Object.prototype.__proto__===null);//true
//Person.prototype.constructor 属性指向构造函数本身
console.log(Person.prototype.constructor===Person);//true
console.log(stu1)//打印如下:
//stur不包含prototype属性
由上面的原型组成的链状结构叫原型链
最新文章
- 单页web应用是什么?它又会给传统网站带来哪些好处?
- python 数据分析--词云图,图形可视化美国竞选辩论
- java常用基础知识点 (持续追加)
- oracle查看当前用户权限
- android知识体系
- java调用Http请求 -HttpURLConnection学习
- 【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query
- 教你配置linux服务器登陆欢迎信息
- P140、面试题24:二叉搜索树的后序遍历序列
- WPF+AE开发小结--TOCControl右键菜单删除图层
- Unity游戏中使用贝塞尔曲线
- ping不通公网ip时路由器设置
- 记一次Django报错Reverse for 'indextwo' with no arguments not found. 1 pattern(s) tried: ['$index/$']
- Html5前端笔记
- Immediate Decodability HDU1305
- Hierarchical Question-Image Co-Attention for Visual Question Answering
- js 根据相对路径url获得完整路径url
- SharePoint Online 创建列表库
- git for c#, commit本地,pushserver
- TCP requires two packet transfers to set up the connection before it can send data