万象更新 Html5 - es6 类: prototype

news/2024/9/24 11:31:19

源码 https://github.com/webabcd/Html5
作者 webabcd

万象更新 Html5 - es6 类: prototype

示例如下:

es6\src\class\prototype.js

// es5 中的 prototype(原型)function Person(name) {// 私有变量let abc = "";// 私有函数let xyz = function() {}// 对象属性this.name = name;// 对象方法this.hello = function() {return "hello: " + this.name;}
}
// 类属性
Person.isMale = true;
// 类方法
Person.run = function(name) {return "run: " + name;
};
// 原型方法(相当于对象方法)
Person.prototype.walk = function() {return "walk: " + this.name;
};
// 原型方法(如果有重名的对象方法,则会执行对象方法,而不会执行原型方法)
Person.prototype.hello = function() {return "hello(prototype): " + this.name;
};
let person = new Person("webabcd");
console.log(person.hello(), person.walk(), Person.run("webabcd"), Person.isMale);
// hello: webabcd walk: webabcd run: webabcd true// 通过原型为类增加对象属性和对象方法
Person.prototype.age = 100; // 为 Person 增加对象属性 age,其默认值为 100
Person.prototype.sleep = function() { // 为 Person 增加对象函数 sleep()return "sleep: " + this.name + this.age;
};
person.age = "40";
console.log(person.sleep());
// sleep: webabcd40// 为指定对象增加属性和方法(仅指定对象有效)
person.salary = 1000;
person.info = function() {return "info: " + this.name + this.salary;
}
person.salary = "2000";
console.log(person.info());
// info: webabcd2000// Object.setPrototypeOf() - 将指定对象的原型设置为另一个对象
function A() {this.name = "webabcd";
}
let b = {age:40
};
A.prototype.salary = 1000;
let a = new A();
console.log(a.name, a.age, a.salary); // webabcd undefined 1000
Object.setPrototypeOf(a, b); // 将 a 的原型设置为 b(不影响对象属性和对象方法)
console.log(a.name, a.age, a.salary); // webabcd 40 undefined// Object.getPrototypeOf() - 获取指定对象的原型对象
// isPrototypeOf() - 用于判断当前原型是否是指定对象的原型
function C() {this.name = "webabcd";
}
let c = new C();
let c_prototype = Object.getPrototypeOf(c);
c_prototype.age = 100;
console.log(c.name, c.age, new C().age, c_prototype.isPrototypeOf(c));
// webabcd 100 100 true// 注:能用 Reflect 的方法就用 Reflect 的,而不要再用 Object 的了

源码 https://github.com/webabcd/Html5
作者 webabcd

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/64106.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

app手机元素定位方式

方式一:采用uiautomatorviewer元素定位 ①这个定位方式有缺陷也就是它只能识别Android8以下的系统,8及以上的系统就无法定位了 ②位置:这个工具是位于安装的SDK下的tools文件下,我的安装路径如下③双击打开这个工具就行,再运行的页面点击 方式二:安装Appium-Inspector ①下…

从0到0.1学习实践盒子模型

从0到0.1学习实践盒子模型 盒子模型概念 预备知识margin:外边距【两个元素之间的距离】 border:边框 padding:内边距【内容区域和边框距离】 height:文本高度 width:文本宽度content-box (形象图片)content-box--只计算内容区域的宽度和高度,边框和内边距不算在内。 .b…

Leetcode 445. 两数相加 II

1.题目基本信息 1.1.题目描述 给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。 你可以假设除了数字 0 之外,这两个数字都不会以零开头。 1.2.题目地址 https://leetcode.cn/problems/add…

Windows命令:时间延迟命令

延迟一段时间再执行下一条命令”。 一、利用ping实现延迟命令 这种延时手段是不精确的,因为每一次ping通的延迟不一样。 1、示例chcp 65001 @echo off echo 延时前:%time% ping /n 3 127.0.0.1 >nul echo 延时后:%time% pause 参数/n表示ping通的次数。127.0.0.1是本机ip…

电力施工作业绝缘手套识别系统

电力施工作业绝缘手套识别系统对电力作业人员在电力设备上进行施工作业时是否佩戴绝缘手套进行识别分析,当电力施工作业绝缘手套识别系统检测到作业人员未佩戴绝缘手套时立即抓拍存档同步回传给后台监控人员,提醒相关人员及时制止,及时规避更危险的触电事故发生。电力施工作…

sicp每日一题[2.24-2.27]

2.24-2.26没什么代码量,所以跟 2.27 一起发吧。Exercise 2.24Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as…

ansible-cmdb简单使用

1、安装 官方:https://ansible-cmdb.readthedocs.io/en/latest/ wget https://github.com/fboender/ansible-cmdb/releases/download/1.27/ansible-cmdb-1.27-2.noarch.rpm yum -y install ./ansible-cmdb-1.27-2.noarch.rpm2、使用 首先,为你的主机生成 Asible 输出: mkdir…

ToEasy利用99元阿里云服务器内网穿透的实操过程

一、准备工作: 1、阿里云99元服务器(安装Windows)或者其他windows云服务器 2、frp内网穿透软件 3、数据库MSSQL 2014绿色版 4、ToEasy服务器和客户端软件 二、内网穿透设置 解压frp软件后,打开配置文件frps.toml和frpc.toml进行设置。 1、服务端(frps.toml)#bindAddr = &…