[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. 示例一: ...
随机推荐
- [BZOJ5293][BJOI2018]求和(倍增)
裸的树上倍增. #include<cstdio> #include<cstring> #include<algorithm> #define rep(i,l,r) ...
- 【贪心】【字典树】Gym - 101466A - Gaby And Addition
题意:定义一种无进位加法运算,给你n个正整数,问你取出两个数,使得他们加起来和最大/最小是多少. 无进位加法运算,其实是一种位运算,跟最大xor那个套路类似,很容易写出对于每个数字,其对应的最优数字是 ...
- 利用Pastezort渗透win7
下载Pastezort git clone https://github.com/ZettaHack/PasteZort.git 给Pastezort文件夹提升权限 /root/PasteZort/ ...
- Codeforces Round #102 (Div. 1) A. Help Farmer 暴力分解
A. Help Farmer 题目连接: http://www.codeforces.com/contest/142/problem/A Description Once upon a time in ...
- PIXIV 爬取国际前100名代码
PYTHON爬虫 爬取PIXIV国际前100名的代码 代码是别人的,那天学习爬虫的时候看到了,写的很厉害~ 学习学习 #coding:UTF-8 __author__ = 'monburan' __v ...
- javascript中的lambda表达式
<!DOCTYPE html> <html> <head> </head> <body> <script> var nubLis ...
- mysql另类分页方法
mysql> limit ,;select found_rows(); +----+-----------------+---------+--------+ | id | rankName | ...
- 多线程_java多线程环境下栈信息分析思路
导读:Java多线程开发给程序带来好处的同时,由于多线程程序导致的问题也越来越多,而且对问题的查找和分析解决对于菜鸟程序原来是是件头疼的事.下面我就项目中使用多线程开发程序过程中遇到的问题做详细的分析 ...
- UCS UTF UTF-7 UTF-8 UTF-16
Unicode也是一种字符编码方法,不过它是由国际组织设计,可以容纳全世界所有语言文字的编码方案.Unicode的学名是"Universal Multiple-Octet Coded Cha ...
- CentOS7LINUX 内核调试符号安装
yum install -y kernel-devel # debuginfo,在CentOS7中需要这样装 sudo vim /etc/yum.repos.d/CentOS-Debuginfo.re ...