当前位置:网站首页>Prototype and prototype chain in JS
Prototype and prototype chain in JS
2022-07-27 05:41:00 【weixin_ forty-six million fifty-one thousand two hundred and si】
1) Prototype 
2) Prototype chain : Find the properties and methods of the object
1、 The object itself cannot find
2、 Constructor find
3、 Object prototype find
4、 Constructor prototype find
5、 Look up in the prototype ------》 Keep looking up , Until I can't find it | So far to find
At the top of the prototype chain is null, The object cannot be found. The attribute is undefined
function Fun(){
this.test1=function(){
console.log('test1');
}
}
Fun.prototype.test2=function(){
console.log('test2');
}
var fun=new Fun()
fun.test1()// Own attributes
fun.test2()// Found on the prototype chain
console.log(fun.toString());// Constructors Fun Prototype object , It's also object An instantiated object of ,
// Follow this prototype chain to find Object Upper toString Method

3)JS Modification and rewriting of prototype in
modify
function Person(name){
this.name=name
}
Person.prototype.getName=function(){
console.log(ths.name);
}
var p=new Person(' Zhang San ')
console.log(p.__proto__===Person.prototype);//true
console.log(p.constructor);//ƒ Person(name){this.name = name}
console.log(p.__proto__===p.constructor.prototype);//true
rewrite
Direct to Person The prototype object of is assigned with an object ,p The constructor of points to the root constructor object
function Person(name){
this.name=name
}
// Rewriting prototype
Person.prototype={
getName:function(){
console.log(this.name);
}
}
var p=new Person(' Zhang San ')
console.log(p.__proto__===Person.prototype);//true
console.log(p.__proto__);//{getName:f}
console.log(p.constructor.prototype);//{costructor:...}
console.log(p.__proto__===p.constructor.prototype);//false
p.constructor=Person
console.log(p.__proto__ === p.constructor.prototype);//true
topic :
边栏推荐
猜你喜欢
随机推荐
Differences between IsNaN and number.isnan in JS
User login - and create and verify SMS verification code
函数和箭头函数
一本通1319——排队接水
How to judge whether an object is empty in JS
Ubuntu:安装PostgreSQL
一本通1251——仙岛求药(广度优先搜索)
dirsearch[目录扫描工具]
程序环境和预处理(下):#define、#undef、命令行编译、条件编译、文件包含(超全整理,建议收藏!!!
[MRCTF2020]PYWebsite 1
编辑删除用户
Trying to evolve_ My first CSDN blog
页面布局中元素定位的几种方式
==,===,Object.is
Self understanding and thinking
背景图片相关应用-铺满,自适应
深度优先搜索(dfs)简介
「PHP基础知识」PHP中实现数学运算
攻防世界-lottery
JS中如何判断一个对象是空对象









