当前位置:网站首页>JS foundation 14
JS foundation 14
2022-04-22 12:54:00 【Pengcheng 933】
Prototype object supplement
Archetypal model
Definition : Each function is created , Will create a prototype attribute , This property is an object , That is, the constructor creates the prototype of the object , The value assigned to the object instance in the constructor , Can be directly assigned to their prototype
<script> function Person(){
Person.prototype.name='jack' Person.prototype.age=20 Person.prototype.say=function(){
console.log(this.name,' speak ');
}
}
let person1=new Person()
person1.say()
// result jack speak
</script>
Share instance object
<script> function Person(name,age){
this.name=name,
this.age=age
}
// Person.prototype.say=function(){
// console.log(this.name,' speak ');
// }
// let p1=new Person('jack',20) // p1.say() // The comments can also be replaced by the following code Person.prototype={
say:function(){
console.log(this.name,' speak ');
}
}
let p1=new Person('jack',20)
p1.say()
</script>
Understand the prototype
Just create a function , There is one prototype Attribute points to prototype object , Prototype objects also have a constructor Pointing function , That is to say Person.prototype.constructor===Person
<script> function Person(){
Person.prototype.name='jack' Person.prototype.age=20 Person.prototype.say=function(){
console.log(this.name,' speak ');
}
}
console.log(typeof Person.prototype);
// result Object
// That is to say Person A prototype is an object
console.log(Person.prototype);
// result {
constructor:f}
// The prototype object has a method pointing to the constructor , This function contains Person The properties and methods in
console.log(Person.prototype.__proto__===Object.prototype);
// result true
// That is, the prototype of any function and the prototype of the object Object The prototype is pointing to the same
console.log(Person.prototype.__proto__.__proto__);
// result null
// That is to say Object The prototype of the prototype is empty , That is to say Object The prototype ends
let person1=new Person()
let person2=new Person()
console.log(person1.__proto__);
// result {
name: 'jack', age: 20, say: ƒ, constructor: ƒ}
// That is, the instance object passes through __proto__ Reach the prototype of the constructor
console.log(person1.__proto__===person2.__proto__);
// result true
// That is, the prototype of the instance object points to the same
</script>
Set and get prototype objects
- getPrototypeOf() Get prototype object
<script> function Person(){
Person.prototype.name='jack' Person.prototype.age=20 Person.prototype.say=function(){
console.log(this.name,' speak ');
}
}
let person1=new Person()
let person2=new Person()
console.log(Object.getPrototypeOf(person1));
// result {
name: 'jack', age: 20, say: ƒ, constructor: ƒ}
console.log(Object.getPrototypeOf(person1.name));
result jack
</script>
- setPrototypeOf() Set prototype object
<script> let person={
name:'jack'
}
let age={
age:20
}
Object.setPrototypeOf(person,age)
console.log(person.age);
// result 20
</script>
- adopt setPrototypeOf() Setting the prototype object and modifying the inheritance relationship will cause performance degradation , So it's commonly used Object.create() Set prototype object
<script> let age={
age:20
}
let person=Object.create(age)
person.name='jack'
console.log(person.age);
// result 20
</script>
Prototype chain
Definition :2 Or 2 More than one prototype , By hiding the prototype __proto__ Properties are connected , Form a chain of deconstructions connected
Prototype chain action : Property method inheritance
Properties and methods in multiple prototype objects , Instance objects are accessed through the prototype chain, or instance objects find properties and methods along the prototype chain
In a real object-oriented language , Before defining an object , First create the object type
class Person{
constructor(name,age){
this.name=name
this.age=age
}
say(){
console.log(this.name,' speak ')
}
}
let P1=new Person('jack',18)
// result jack speak
notes : It's just constructor syntax sugar , stay JS The compiler is still a constructor
tab ( First create the object type , Redefine the type )
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{
padding: 0;
margin: 0;
}
.tab{
width: 600px;
height: 600px;
margin: 0 auto;
}
ul{
width: 600px;
height: 50px;
}
ul li{
width: 198px;
height: 50px;
background-color: pink;
float: left;
list-style: none;
border: 1px solid orange;
text-align: center;
line-height: 50px;
}
.active{
background-color: orange;
}
.box{
width: 600px;
height: 550px;
background-color: skyblue;
text-align: center;
line-height: 550px;
}
</style> </head> <body> <div class="tab" id="tab1"> <ul> <li> Options 1</li> <li> Options 2</li> <li> Options 3</li> </ul> <div class="box"> Options 1</div> </div> <script> class Tab{
constructor(id){
this.root=document.querySelector(id)
this.uls=this.root.querySelectorAll('ul>li')
this.box=this.root.querySelector('.box')
}
onClear(){
this.uls.forEach(item => {
item.classList.remove('active')
});
}
onTab(){
let _this=this
for(let i=0;i<this.uls.length;i++){
this.uls[i].οnclick=function(){
_this.onClear()
_this.uls[i].classList.add('active')
let content=this.innerHTML
_this.box.innerHTML=content
}
}
}
}
let t1=new Tab('#tab1')
t1.onTab()
</script>
</body>
</html>
版权声明
本文为[Pengcheng 933]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221222462860.html
边栏推荐
- 51单片机案例(1)——利用DS1302实现实时时钟和可调时钟的功能
- VR全景婚礼给你别样的浪漫,记录婚礼的甜蜜瞬间
- Solve the problem that CMD commands cannot be executed continuously
- 制裁压力下,俄罗斯计划通过友好国家平行进口电子产品
- R语言ggplot2可视化:计算dataframe中每个数据列缺失值的个数、使用堆叠的条形图(Stacked Barplot)可视化每个数据列的缺失值的情况(设置坐标轴为百分比以显示缺失值的比例)
- Daily question series: common substring calculation
- Male female pairing
- Generation process of executable file
- Under the pressure of sanctions, Russia plans to import electronic products in parallel through friendly countries
- 分享一下自己最近写项目遇到的小问题
猜你喜欢

Male female pairing

Daily question series: common substring calculation

JS native code to achieve three-level linkage effect

Smart cultural tourism is gradually digitalized, and VR panorama promotes the integrated development of cultural tourism

The ABAQUS model of standardized steel box girder is established, and the plug-in of RSG is used for secondary development

数学——协方差

21. merge two ordered linked lists

IDE导入项目

可执行文件的生成过程

The shortest distance of dynamic programming characters
随机推荐
Will the slice, replace, touppercase and repeat methods of string change the original string
2022年四月21号S S M 框架整合@第一课 S S M 环境配置&重在实操。实操中总结。这里不展示结果。
Under the pressure of sanctions, Russia plans to import electronic products in parallel through friendly countries
Ros2 learning notes (V) learn ros2 parameters from turnlesim
MySQL 5.0安装教程图解详细教程
11. Container with the most water
线程相关问题
R语言ggplot2可视化:ggplot2可视化将图像的图例(legend)移动到图像内部、自定义图例所处的位置
今日睡眠质量记录76分
Five ways to get database connection: have you really got it?
Daily question series: common substring calculation
New ideas for smart home (2022)
Simple deployment of microservices
VR全景婚礼给你别样的浪漫,记录婚礼的甜蜜瞬间
Matlab bridge span combination problem GUI Graphical interface is completed
标准化钢箱梁abaqus模型建立,使用RSG的插件二次开发
abaqus RSG插件二次开发(二)
VR panorama truly restores the driving school environment, and VR live shows the hard power of the driving school
Give yourself the possibility of success
51 single chip microcomputer case (1) -- using DS1302 to realize the functions of real-time clock and adjustable clock