<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>结算程序</h1>
<p>
<input type="text" placeholder="原价" id="money" />
</p>
<p>
<select id="level">
<option value="0">普通顾客</option>
<option value="1">VIP会员</option>
<option value="2">金卡会员</option>
<option value="3">砖石会员</option>
</select>
</p>
<p>
<input type="button"id="btn" value="结算" />
</p>
<p id="result"></p>
<script src="test_4es5.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>

原型定义对象的方式缺点是较为麻烦,但是好处是创建对象里面不包括变量和方法的声明

原型定义的对象创建的速度快,占用内存小,推荐使用

 function Calculator() {

 }
Calculator.prototype.money = null;
Calculator.prototype.level = null;
Calculator.prototype.pay = function() {
if(this.level == 0) {
return this.money;
} else if(this.level == 1) {
return `${this.money * 0.96}元`;
} else if(this.level == 2) {
return this.money * 0.92;
} else if(this.level == 3) {
return this.money * 0.85;
} else {
throw "会员等级错误";
}
} function DoubleElevenCalculator() { };
DoubleElevenCalculator.prototype = new Calculator();
DoubleElevenCalculator.prototype.pay = function() {
if(this.level == 0) {
var num = Math.floor(this.money / 200);
return `实际需支付${this.money}元,赠送${num}张50元代金券`;
} else if(this.level == 1) {
var num = Math.floor(this.money / 500);
return `实际需支付${this.money*0.9}元,赠送${num}张100元餐券`
} else if(this.level == 2) {
var num = Math.floor(this.money / 300);
return `实际需支付${this.money*0.85}元,赠送${num}张100元代金券`;
} else if(this.level == 3) {
var num = Math.floor(this.money / 200);
return `实际需支付${this.money*0.8}元,赠送${num}张100元代金券`;
} else {
throw "会员等级错误";
}
} var btn = document.querySelector("#btn");
btn.addEventListener("click", function() {
//var calculator = new Calculator();
var calculator = new DoubleElevenCalculator();
calculator.money = document.querySelector("#money").value;
var money = new Number(money);
calculator.level = document.querySelector("#level").value;
var level = new Number(level);
var temp = calculator.pay();
document.querySelector("#result").innerHTML = temp; });
 
 /*
* 发法一new Object()
* 对象只能调用一次,不能重复利用,也不能被继承
*/ /*
var plane = new Object();
plane.speed=800;
plane.fly=function(){
console.log("飞行速度"+this.speed);
}
plane.fly();
*/ /*
* 方法二json
*/
/*
var plane={
speed:800,
fly:function(){
console.log("飞行速度"+this.speed);
}
}
plane.fly();
*/ /*
* 方法3函数模拟类
* 不能继承
*/
/*
function Plane(){
this.speed=800;
this.fly=function(){
console.log("飞行速度"+this.speed);
}
}
var plane =new Plane();
plane.fly();
*/ /*
* 方法4 最好
*/
function Plane() { }
Plane.prototype.speed = null;
Plane.prototype.fly = function() {
console.log("飞行速度" + this.speed);
}
var plane = new Plane();
plane.speed = 800;
plane.fly(); function FightPlane() { }
FightPlane.prototype = new Plane(); //继承父类prototype所有的内容
FightPlane.prototype.fly = function() {
console.log("战斗机飞行速度" + this.speed)
}
plane = new FightPlane();
plane.speed = 2000;
plane.fly();

JS5模拟类的更多相关文章

  1. js模拟类

    ECMAScript6已经支持了class,但之前版本都不支持类,但是可以通过一些方法来模拟类. js中的类,既是重点,也是难点,很多时候都感觉模棱两可. 首先强调一下js中很重要的3个知识点:thi ...

  2. JavaScript 高级篇之闭包、模拟类,继承(五)

    本篇主要分享我对闭包的理解及使用闭包完成私有属性.模拟类.继承等,结合大量例子,希望大家能快速掌握!首先让我们先从一些基本的术语开始吧     一.javascript中的闭包 1.我们一起先来理解什 ...

  3. poj1068解题报告(模拟类)

    POJ 1068,题目链接http://poj.org/problem?id=1068 题意: 对于给出给出的原括号串S,对应两种数字密码串P.W: S         (((()()()))) P- ...

  4. 在 JavaScript 中使用构造器函数模拟类

    今天,我们要讲的是在 JavaScript 中使用构造器函数(construcor function)模拟类. 构造器函数简介 你可以使用 ES6 的 class 关键字来实现类,不过我建议你使用传统 ...

  5. 【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态

    一.简介 Lua是一门非常强大.非常灵活的脚本语言,自它从发明以来,无数的游戏使用了Lua作为开发语言.但是作为一款脚本语言,Lua也有着自己的不足,那就是它本身并没有提供面向对象的特性,而游戏开发是 ...

  6. [原创]Javascript模拟“类”的综合实现方式以及部分细节【截至ES6】

    [原创]Javascript模拟“类”的综合实现方式以及部分细节[截至ES6] 前言   最近几个旧项目里使用的图片编辑插件出现Bug, 经Review 后确定需要在其内外均做些改动,但是头疼的发现部 ...

  7. [LeetCode] 数学计算模拟类问题:加法,除法和幂,注意越界问题。题 剑指Offer,Pow(x, n) ,Divide Two Integers

    引言 数学计算的模拟类题目,往往是要求实现某种计算(比如两数相除),实现的过程中会有所限定,比如不允许乘法等等. 这类题目首先要注意计算过程中本身的特殊情况.比如求相除,则必须首先反映过来除数不能为0 ...

  8. 【转载】【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态

    [游戏开发]在Lua中实现面向对象特性——模拟类.继承.多态   阅读目录 一.简介 二.前提知识 三.Lua中实现类.继承.多态 四.总结 回到顶部 一.简介 Lua是一门非常强大.非常灵活的脚本语 ...

  9. javascript模拟类的最佳实践

    1:怎样模拟一个类 在sencha touch2 系列里面定义一个类和new出这个类的对象 Ext.define( "Animal", { config: { name: null ...

随机推荐

  1. ArrayList ConcurrentModificationException

    1.ConcurrentModificationException ConcurrentModificationException 出现在使用 ForEach遍历,迭代器遍历的同时,进行删除,增加出现 ...

  2. ERROR! MySQL server PID file could not be found!的解决方法

    启动MySQL服务 [root@test vhosts]# /etc/init.d/mysqld restart 提示错误: ERROR! MySQL server PID file could no ...

  3. Python面试题之python是一种什么语言及优缺点

    1.说说python是一种什么语言? 参考答案:python是一门动态解释性的强类型定义语言 编译型vs解释型 编译型优点:编译器一般会有预编译的过程对代码进行优化.因为编译只做一次,运行时不需要编译 ...

  4. DSCP 与IP 优先级IP优先级

    在IPv4的报文头中,TOS字段是1字节,如下图所示.根据RFC1122的定义,IP优先级(IPPrecedence)使用最高3比特(第0-2比特).+++++++++++++++++++++++++ ...

  5. HDU1789 Doing Homework again

    基础单调队列: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...

  6. python pip安装方法

    1.python安装,最好是按照32位的版本,64位版本有的时候出现奇怪问题. 2.python安装完成后,需要在系统的环境变量"path"中增加路径设置. 3.一般情况下,使用p ...

  7. (转载)RESTful架构风格下的4大常见安全问题

    转载自<RESTful架构风格下的4大常见安全问题>,作者:马伟 伴随着RESTful架构风格的大量应用微服务架构的流行,一些本来难以察觉到的安全问题也逐渐开始显现出来.在我经历过的各种采 ...

  8. Mac使用nginx+rtmp服务器

    一.安装Homebrow 已经安装了brow的可以直接跳过这一步.执行命令 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/H ...

  9. Nginx配置文件中文详解

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...

  10. 【计算机网络基础】数据交换技术和多路复用技术的正(nao)确(can)打开方式

    交换的作用   数据交换是计算机网络中两个终端进行数据传输的方式,它又可以分成两种类型:电路交换和分组交换.很显然,问题的核心在于“交换”,那么我们首先要思考的是:交换的作用是什么?   “交换”的作 ...