[Javascript] Prototype, hasOwnProperty(), valueOf() and toString() methods.
Sometime, use can rewrite the toString , valueOf method to make those function more useful:
For exmaple, we can make valueOf() function to calcualte the sum, and then use toString method to display the information of the object we create.
var Tornado = function(category, affectedAreas, windGust){
this.category = category;
this.affectedAreas = affectedAreas;
this.windGust = windGust;
};
var cities = [["Kansas City", 46310],["Topeka", 127939],["Lenexa", 49398]];
var twister = new Tornado("F5", cities, 220);
cities.push(["Olathe", 130045]);
twister.toString();
Tornado.prototype.toString = function(){
var list = "";
for(var i = 0; i< this.affectedAreas.length; i++){
if(i < this.affectedAreas.length-1){
list = list + this.affectedAreas[i][0] + ", ";
}else{
list = list + "and "+ this.affectedAreas[i][0];
}
}
return "This tornado has been classified as an " + this.category+
", with wind gusts up to "+ this.windGust+ "mph. Affected areas are:"+
list+", potentially affecting a population of "+ this.valueOf() + ".";
};
Tornado.prototype.valueOf = function(){
var sum = 0;
for(var i = 0; i < this.affectedAreas.length; i++){
sum += this.affectedAreas[i][1];
}
return sum;
}
Object.prototype.findOwnProperty = function(propName){
var currentObject = this;
while(currentObject !== null){
if(currentObject.hasOwnProperty(propName)){
return currentObject;
}else{
currentObject = currentObject.__proto__;
}
}
return "No property found!";
}
twister.findOwnProperty("valueOf");
[Javascript] Prototype, hasOwnProperty(), valueOf() and toString() methods.的更多相关文章
- 深入理解Javascript中的valueOf与toString
基本上,javascript中所有数据类型都拥有valueOf和toString这两个方法,null除外.它们俩解决javascript值运算与显示的问题,本文将详细介绍,有需要的朋友可以参考下. t ...
- JavaScript中的valueOf与toString方法
基本上,所有JS数据类型都拥有valueOf和toString这两个方法,null除外.它们俩解决javascript值运算与显示的问题. JavaScript 的 valueOf() 方法 valu ...
- Javascript中的valueOf与toString
基本上,javascript中所有数据类型都拥有valueOf和toString这两个方法,null除外.它们俩解决javascript值运算与显示的问题,本文将详细介绍,有需要的朋友可以参考下. t ...
- Javascript中valueOf与toString区别
前言 基本上,所有JS数据类型都拥有这两个方法,null除外.它们俩解决JavaScript值运算与显示的问题,重写会加大它们调用的优化. 测试分析 先看一例:var aaa = { i: 10, ...
- JavaScript的valueOf和toString
深度好文 http://www.cnblogs.com/coco1s/p/6509141.html 知识要点 不同对象调用valueOf和toString的顺序不一样 高阶函数的使用,替代for循环 ...
- JavaScript Prototype in Plain Language
非常好的文章: http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/ jan. 25 2013 14 ...
- illustrating javascript prototype & prototype chain
illustrating javascript prototype & prototype chain 图解 js 原型和原型链 proto & prototype func; // ...
- Object.prototype.hasOwnProperty与Object.getOwnPropertyNames
Object.prototype.hasOwnProperty() 所有继承了 Object 的对象都会继承到 hasOwnProperty 方法.这个方法可以用来检测一个对象是否含有特定的自身属性: ...
- 理解JAVASCRIPT 中hasOwnProperty()和isPrototypeOf的作用
hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员.格式如下: 1. 示例一: ...
随机推荐
- 【BZOJ 3640】JC的小苹果 (高斯消元,概率DP)
JC的小苹果 Submit: 432 Solved: 159 Description 让我们继续JC和DZY的故事. “你是我的小丫小苹果,怎么爱你都不嫌多!” “点亮我生命的火,火火火火火!” 话 ...
- Python进阶篇:文件系统的操作
通过一个例子来熟悉文件的基本操作:创建文件,读取文件,修改文件,删除文件,重命名文件,判断文件是否存在 ''' 编写可供查询的员工信息表--学号 姓名 年龄 班级 1. 提供格式化查询接口 2. 允许 ...
- Contest Reviews(Updating)
现在每天至少一套题又不太想写题解…… 那就开个坑总结下每场的失误和特定题目的技巧吧 2018.8.25[ZROI] T3传送门 T1:找规律找崩了…… 最好不要一上来就钻进大讨论,先想有没有普适规律 ...
- Codeforces Round #357 (Div. 2) A. A Good Contest 水题
A. A Good Contest 题目连接: http://www.codeforces.com/contest/681/problem/A Description Codeforces user' ...
- Bootstrap_CSS概览
在这一章中,我们将讲解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML 5 文档类型(Doctype) Bootstrap 使用了一些 H ...
- PostgreSQL修改数据库目录/数据库目录迁移
说明:以9+版本为例,10+的版本只要把目录替换一下即可.迁移目录肯定是要停服的! 1.在数据库软件安装之后,初始化数据库时候,可以指定初始化时创建的数据库的默认文件路径 /usr/local/pgs ...
- Delphi 调用SQL Server 2008存储过程
1.表结构如下(预算数据明细表): CREATE TABLE [dbo].[BA_FeeDetail]( [ID] [int] IDENTITY(1,1) NOT NULL, [FeeDeptID] ...
- C语言 const, static, static const 的区别
基本定义: const 就是只读的意思,只在声明中使用;static 一般有2个作用,规定作用域和存储方式. 对于局部变量, static规定其为静态存储方式, 每次调用的初始值为上一次调用的值,调 ...
- 关于linux系统端口查看和占用的解决方案
原文:http://www.2cto.com/os/201411/355959.html 一直以来,在处理linux服务器的过程中,经常会遇到一个问题,有时候kill掉进程之后,端口被占用,新的进程一 ...
- FMXUI
FMXUI GITHUB: https://github.com/yangyxd/FMXUI FMXUI (YangYxd) [简介] FMXUI的开发忠旨是发掘FMX界面设计的优点,再整合进入And ...