当前位置:网站首页>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 :
边栏推荐
猜你喜欢

Exit login and JSX display

元素显示模式:块级,行内,行内块,嵌套规范,显示模式转换

流程控制-分支

GalleryCMS下载安装与配置

一文读懂Elephant Swap的LaaS方案的优势之处

Share a multiple-choice question about variables (including global variables, local variables, the scope of variables, and life cycle knowledge points)

First knowledge of C language - why does every C program have a main function

Asynchronous data SMS verification code

Trying to evolve_ My first CSDN blog

「PHP基础知识」PHP中的注释
随机推荐
下载url-loader,用limit指定图片大小后,显示不出图片
一本通1251——仙岛求药(广度优先搜索)
维持登录,路由跳转
Page configuration
JS中for...of和for...in的区别
node 安装调试
Pytorch installation new pit
Qsort - the sorting function in C language (with void*, callback function knowledge points
User registration login
Introduction to C language
while循环
JS中arguments类数组
每周学习总结
「PHP基础知识」定界符的使用
Introduction to C language functions
JS中原型及原型链
Program environment and preprocessing (Part 1): how does a program run successfully?
MD5 password encryption
What are Dom and BOM in JS
Sharing force buckle-189. Three solutions of rotation array