Typescript中的类 Es5中的类和静态方法和继承(原型链继承、对象冒充继承、原型链+对象冒充组合继承)
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="description" content="">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script>
- // es5里面的类
- //1.最简单的类
- // function Person(){
- // this.name='张三';
- // this.age=20;
- // }
- // var p=new Person();
- // alert(p.name);
- //2、构造函数和原型链里面增加方法
- // function Person(){
- // this.name='张三'; /*属性*/
- // this.age=20;
- // this.run=function(){
- // alert(this.name+'在运动');
- // }
- // }
- // //原型链上面的属性会被多个实例共享 构造函数不会
- // Person.prototype.sex="男";
- // Person.prototype.work=function(){
- // alert(this.name+'在工作');
- // }
- // var p=new Person();
- // // alert(p.name);
- // // p.run();
- // p.work();
- //3类里面的静态方法
- // function Person(){
- // this.name='张三'; /*属性*/
- // this.age=20;
- // this.run=function(){ /*实例方法*/
- // alert(this.name+'在运动');
- // }
- // }
- // Person.getInfo=function(){
- // alert('我是静态方法');
- // }
- // //原型链上面的属性会被多个实例共享 构造函数不会
- // Person.prototype.sex="男";
- // Person.prototype.work=function(){
- // alert(this.name+'在工作');
- // }
- // var p=new Person();
- // p.work();
- // //调用静态方法
- // Person.getInfo();
- // 4、es5里面的继承 对象冒充实现继承
- // function Person(){
- // this.name='张三'; /*属性*/
- // this.age=20;
- // this.run=function(){ /*实例方法*/
- // alert(this.name+'在运动');
- // }
- // }
- // Person.prototype.sex="男";
- // Person.prototype.work=function(){
- // alert(this.name+'在工作');
- // }
- // //Web类 继承Person类 原型链+对象冒充的组合继承模式
- // function Web(){
- // Person.call(this); /*对象冒充实现继承*/
- // }
- // var w=new Web();
- // // w.run(); //对象冒充可以继承构造函数里面的属性和方法
- // w.work(); //对象冒充可以继承构造函数里面的属性和方法 但是没法继承原型链上面的属性和方法
- // 5、es5里面的继承 原型链实现继承
- // function Person(){
- // this.name='张三'; /*属性*/
- // this.age=20;
- // this.run=function(){ /*实例方法*/
- // alert(this.name+'在运动');
- // }
- // }
- // Person.prototype.sex="男";
- // Person.prototype.work=function(){
- // alert(this.name+'在工作');
- // }
- // //Web类 继承Person类 原型链+对象冒充的组合继承模式
- // function Web(){
- // }
- // Web.prototype=new Person(); //原型链实现继承
- // var w=new Web();
- // //原型链实现继承:可以继承构造函数里面的属性和方法 也可以继承原型链上面的属性和方法
- // //w.run();
- // w.work();
- // 6、 原型链实现继承的 问题?
- // function Person(name,age){
- // this.name=name; /*属性*/
- // this.age=age;
- // this.run=function(){ /*实例方法*/
- // alert(this.name+'在运动');
- // }
- // }
- // Person.prototype.sex="男";
- // Person.prototype.work=function(){
- // alert(this.name+'在工作');
- // }
- // var p=new Person('李四',20);
- // p.run();
- // function Person(name,age){
- // this.name=name; /*属性*/
- // this.age=age;
- // this.run=function(){ /*实例方法*/
- // alert(this.name+'在运动');
- // }
- // }
- // Person.prototype.sex="男";
- // Person.prototype.work=function(){
- // alert(this.name+'在工作');
- // }
- // function Web(name,age){
- // }
- // Web.prototype=new Person();
- // var w=new Web('赵四',20); //实例化子类的时候没法给父类传参
- // w.run();
- // // var w1=new Web('王五',22);
- //7.原型链+对象冒充的组合继承模式
- // function Person(name,age){
- // this.name=name; /*属性*/
- // this.age=age;
- // this.run=function(){ /*实例方法*/
- // alert(this.name+'在运动');
- // }
- // }
- // Person.prototype.sex="男";
- // Person.prototype.work=function(){
- // alert(this.name+'在工作');
- // }
- // function Web(name,age){
- // Person.call(this,name,age); //对象冒充继承 实例化子类可以给父类传参
- // }
- // Web.prototype=new Person();
- // var w=new Web('赵四',20); //实例化子类的时候没法给父类传参
- // // w.run();
- // w.work();
- // // var w1=new Web('王五',22);
- //8、原型链+对象冒充继承的另一种方式
- function Person(name,age){
- this.name=name; /*属性*/
- this.age=age;
- this.run=function(){ /*实例方法*/
- alert(this.name+'在运动');
- }
- }
- Person.prototype.sex="男";
- Person.prototype.work=function(){
- alert(this.name+'在工作');
- }
- function Web(name,age){
- Person.call(this,name,age); //对象冒充继承 可以继承构造函数里面的属性和方法、实例化子类可以给父类传参
- }
- Web.prototype=Person.prototype;
- var w=new Web('赵四',20); //实例化子类的时候没法给父类传参
- w.run();
- // w.work();
- // var w1=new Web('王五',22);
- </script>
- </head>
- <body>
- </body>
- </html>
Typescript中的类 Es5中的类和静态方法和继承(原型链继承、对象冒充继承、原型链+对象冒充组合继承)的更多相关文章
- 《前端之路》- TypeScript (三) ES5 中实现继承、类以及原理
目录 一.先讲讲 ES5 中构造函数(类)静态方法和多态 1-1 JS 中原型以及原型链 例子一 1-2 JS 中原型以及原型链中,我们常见的 constructor.prototype.**prot ...
- Typescript 学习笔记四:回忆ES5 中的类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- ES5中的类与继承
最近在重新复习TypeScript,看到类这块的时候自然会和ES5中的类写法进行对比加深印象. 发现ES5的类与继承一些细节还是挺多的,时间久了容易忘记,特此记录下. 首先是ES5的类定义,这没什么好 ...
- Es5中的类和静态方法 继承
Es5中的类和静态方法 继承(原型链继承.对象冒充继承.原型链+对象冒充组合继承) // es5里面的类 //1.最简单的类 // function Person(){ // this.name='张 ...
- ES6中的类继承和ES5中的继承模式详解
1.ES5中的继承模式 我们先看ES5中的继承. 既然要实现继承,首先我们得要有一个父类. Animal.prototype.eat = function(food) { console.log(th ...
- ES6中。类与继承的方法,以及与ES5中的方法的对比
// 在ES5中,通常使用构造函数方法去实现类与继承 // 创建父类 function Father(name, age){ this.name = name; this.age = age; } F ...
- ES5中的类
之前小编对于类和类的基本特征(所谓的封装.继承.多态)理解一直不是很到位,同时在实际项目应用中也用的比较少,今天小编就结合ES5中的基本用法和大家聊聊,希望小伙伴会在这篇文章有属于自己的收获,并能够在 ...
- 在ES5中模拟类
1.Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. var _this = Object.create(fn.prototype);这句代码的 ...
- "检索COM类工厂中 CLSID为 {00024500-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误: 80070005" 问题的解决
一.故障环境 Windows 2008 .net 3.0 二.故障描述 调用excel组件生成excel文档时页面报错.报错内容一大串,核心是"检索COM类工厂中 CLSID为 {000 ...
随机推荐
- [牛客网 -leetcode在线编程 -02] minimum-depth-of-binary-tree -树的最短深度
题目描述 题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along ...
- [转]Linux网络 - 数据包的接收过程
转, 原文: https://segmentfault.com/a/1190000008836467 ------------------------------------------------- ...
- 什么是IAP?如何实现IAP?
发布时间:2008-09-01 来源:computer00 分享到: IAP是In Application Programming的首字母缩写,IAP是用户自己的程序在运行过程中对User F ...
- CSS hack整理
浏览器的兼容性一直是个头疼的问题,使用“欺骗”技术可使各个浏览器效果一致,花了些时间整理了各个浏览器的HACK,主要包括IE系列和最新版本的Chrome.Safari.Firefox. Opera,比 ...
- NOIP2018模板总结【数学】
质因数分解 //质因数分解 int prime[MAXN], tim[MAXN], cnt; void Divide(int N) { printf("%d = ", N); fo ...
- 堆优化/zkw线段树优化 dijkstra
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MAXM = 200005 ...
- jumpserver 安装
# CentOS 7 安装jumpserver $ setenforce 0 # 可以设置配置文件永久关闭$ systemctl stop iptables.service$ systemctl st ...
- Hive ACID和事务表支持详解
一.ACID介绍 ACID就是常见数据库事务的四大特性:Atomicity(原子性).Consistency(一致性).Isolation(隔离性).Durability(持久性). 在Hive 0. ...
- YAML_09 脚本调用变量+触发器
ansible]# vim adhttp2.yml --- - hosts: cache remote_user: root vars: server: httpd tasks: ...
- YAML_01 YAML语法和playbook写法
ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible pla ...