V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  seakingii  ›  全部回复第 29 页 / 共 42 页
回复总数  829
1 ... 25  26  27  28  29  30  31  32  33  34 ... 42  
```
//第一步: 使用早期语法定义了一个类"Student",现在的 Class 本质上差不多,可以算是语法糖
function Student() {
//类有个方法
this.helloFromStudent = function () {
console.log("====== call helloFromStudent");
};
}

//实例化类
const inst1 = new Student();
//调用方法
inst1.helloFromStudent();

//打印,这时会看到 "Student"有个下属方法叫"helloFromStudent",同时它的原型是"Prototype",和 Class 语法不同的是,这时打印看到"helloFromStudent"方法没有挂成原型上,实际调用效果是一样的
console.dir(inst1);




//第二步:直接在原型上添加了一个新的方法"method2"
//这也是脚本语言特性,可以动态更改类...
Student.prototype.method2 = function () {
this.helloFromStudent();
console.log("=====call method2");
};

//又构造了一个
const inst2 = new Student();
inst2.method2();

//这时可以看到,直接在 Student 上有个方法 helloFromStudent,在原型上有另一个方法"method2",实际上都能调用...
console.dir(inst2);

console.log(Student.prototype.helloFromStudent); //undefined , helloFromStudent 方法直接定义在 Student,没有定义在原型上
console.log(Student.prototype.method2);

//和我的例子相比,现在的 class 语法把方法都放在 prototype 上了.




//第三步:在原型上定义一个同名方法....
Student.prototype.helloFromStudent = function () {
console.log("====== call helloFromStudent,我是放在原型上的方法");
};

//又构造了一个
const inst3 = new Student();
inst3.method2();
//打印结构,这时会看到有意思的情况:helloFromStudent 方法同时有两个,一个是 Student 的直接下级,一个是在原型上,实际调用时,原型上的不起作用,说明直接下级的优先级高
console.dir(inst3);




//第四步:定义一个方法,删除直接下属的 helloFromMethod
Student.prototype.deleteHelloFromStudent = function () {
//删除直接方法"helloFromStudent"
delete this["helloFromStudent"];
};

//又构造了一个
const inst4 = new Student();
inst4.deleteHelloFromStudent();
inst4.method2();
//打印结构,这时会看到有意思的情况:helloFromStudent 方法只有一个,是在原型上了...然后原型上的 helloFromStudent 又起作用了
console.dir(inst4);
```
Javascript 规定,每一个构造函数都有一个 prototype 属性,指向另一个对象。这个对象的所有属性和方法,都会被构造函数的实例继承。
2021-10-04 21:23:41 +08:00
回复了 matrix67 创建的主题 分享发现 陈天老哥画图是画的真不错
@ReferenceE 迷思不是普通的词组吗?又不是台湾人专用的
没有错,是你看错了
2021-10-04 21:13:47 +08:00
回复了 Livid 创建的主题 Cloudflare Cloudflare 宣布将推出和 S3 对标的存储产品 R2 Object Storage
v2ex 的图片是用什么存储的?
2021-10-04 19:24:55 +08:00
回复了 CapricornLiu 创建的主题 程序员 人到中年, 老哥们, 我要不要踏出这一步?
说错了,是程序员的生意真难做
2021-10-04 19:24:33 +08:00
回复了 CapricornLiu 创建的主题 程序员 人到中年, 老哥们, 我要不要踏出这一步?
NND,程序的生意真难做啊,一个个这么挑...哈哈
2021-10-04 19:04:53 +08:00
回复了 seakingii 创建的主题 V2EX v2ex 社区手工签到第一名!????
竟然还有这么多比我会签到的,到底是你们有病还是我有病...
2021-10-04 14:01:27 +08:00
回复了 seakingii 创建的主题 V2EX v2ex 社区手工签到第一名!????
@wd 纯手工么?

纯手工的话我佩服你...
没必要怼客户.
2021-10-01 21:51:37 +08:00
回复了 chieffo 创建的主题 Vue.js Vite:下一代前端开发与构建工具?大伙使用效果如何?
用着还好,除了比较新,有些小问题,不好找答案.
比 webpack 速度快
@bjzhou1990 是的你没必要.所以别人举报不符合规则的项目是有必要的.

为什么在别人的平台上,以自己为中心?
2021-09-25 14:32:53 +08:00
回复了 honhon 创建的主题 Windows win11 你们会升级么?
等稳定一年之后再升级
2021-09-20 12:48:27 +08:00
回复了 wangbenjun5 创建的主题 知乎 如何从逻辑角度解释蛋生鸡、鸡生蛋问题?
基因突变
2021-09-17 22:17:50 +08:00
回复了 neurocomputing 创建的主题 程序员 练手撸了个 Let's encrypt 的 SSL 证书签发服务
这...注册账号是认真的?
2021-09-17 22:15:13 +08:00
回复了 flycloud 创建的主题 程序员 分享一个 id 自增生成器,依赖于 redis,求大佬帮忙挑挑毛病
最大的问题是用 REDIS
2021-09-16 12:13:54 +08:00
回复了 samin 创建的主题 程序员 多少流量才算高并发 ?
@murmur 桃子不是像屁股么?
1 ... 25  26  27  28  29  30  31  32  33  34 ... 42  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1024 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 36ms · UTC 19:52 · PVG 03:52 · LAX 12:52 · JFK 15:52
Developed with CodeLauncher
♥ Do have faith in what you're doing.