当前位置: 首页 > news >正文

XSS原型与原型链

概念说明

prototype和__proto__均为函数中的一种属性,区别在于prototype是构造函数的属性,用于定义实例共享的属性和方法,__proto__是实例的属性,指向创建它的构造函数的prototype

function Animal(type) {this.type = type;
}// 为构造函数的prototype添加方法
Animal.prototype.speak = function() {console.log(`I'm a ${this.type}`);
};// 创建实例
const dog = new Animal('dog');
const cat = new Animal('cat');// 验证实例的__proto__指向构造函数的prototype
console.log(dog.__proto__ === Animal.prototype);       // true
console.log(cat.__proto__ === Animal.prototype);       // true// 验证构造函数的prototype.constructor指向自身
console.log(Animal.prototype.constructor === Animal);  // true// 实例继承的方法来自prototype
console.log(dog.speak === Animal.prototype.speak);     // true
console.log(cat.speak === Animal.prototype.speak);     // true

原型链的层级结构

const bird = new Animal('bird');// 验证原型链的层级
console.log(bird.__proto__ === Animal.prototype);              // true
console.log(Animal.prototype.__proto__ === Object.prototype);   // true
console.log(Object.prototype.__proto__ === null);               // true(原型链终点)// 通过原型链访问Object的方法
console.log(bird.toString === Object.prototype.toString);       // true
console.log(bird.hasOwnProperty === Object.prototype.hasOwnProperty); // true

改原型对实例的影响

function Fruit(name) {this.name = name;
}const apple = new Fruit('apple');
const banana = new Fruit('banana');// 修改原型前
console.log(apple.color); // undefined// 向原型添加属性
Fruit.prototype.color = 'unknown';// 验证所有实例继承新属性
console.log(apple.color); 
console.log(banana.color); 
console.log(apple.color === Fruit.prototype.color); // true

http://www.lryc.cn/news/593254.html

相关文章:

  • Netty集群方案详解与实战(Zookeeper + Redis + RabbitMQ)
  • LangGraph教程9:LangGraph检查点和Send机制
  • 微信小程序171~180
  • 微信小程序入门实例_____从零开始 开发一个“旅行清单 ”微信小程序
  • 微信小程序——世界天气小助手
  • YOLOv11改进 | RFAConv重塑空间注意力助力性能提升
  • 针对大规模语言模型的上下文工程技术调研与总结(翻译并摘要)
  • The Missing Semester of Your CS Education 学习笔记以及一些拓展知识(三)
  • 华为仓颉编程语言语法简介与示例
  • OpenCV 官翻8 - 其他算法
  • 从 Server.xml 到字节码:Tomcat 内核全景与请求旅程 10 000 字深剖
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘sqlalchemy’问题
  • Jenkins自动化部署.NET应用实战:Docker+私有仓库+SSH远程发布
  • Entity Component System架构
  • 【计算机网络架构】树型架构简介
  • 阶段1--Linux存储管理
  • Azure Bicep 是什么?
  • Vue rem回顾
  • 消息队列与信号量:System V 进程间通信的基础
  • 基于开放API接口采集的定制开发开源AI智能名片S2B2C商城小程序数据整合与增长策略研究
  • C++入门--lesson4
  • react17更新哪些新特性
  • HTTPHTTPSTLSDNSRSA
  • 使用nvm安装node、npm、pnpm以及编译项目教程
  • 编程实现Word自动排版:从理论到实践的全面指南
  • Spring Cloud Gateway与Envoy Sidecar在微服务请求路由中的架构设计分享
  • 第二阶段-第二章—8天Python从入门到精通【itheima】-133节(SQL——DQL——基础查询)
  • 用户中心项目实战(springboot+vue快速开发管理系统)
  • Vue3的definePros和defineEmits
  • Promise入门