第146页 PRIMITIVE WRAPPER TYPES
byhard (byhard.com)
- 章节名:PRIMITIVE WRAPPER TYPES
- 页码:第146页
原类型对应的引用类型:Boolean, Number, String. Three special reference types are designed to ease interaction with primitive values: the Boolean type, the Number type, and the String type. var s1 = “some text”; s1.color = “red”; alert(s1.color); //undefined 为什么? Here, the second line attempts to add a color property to the string s1. However, when s1 is accessed on the third line, the color property is gone. This happens because the String object that was created in the second line is destroyed by the time the third line is executed. The third line creates its own String object, which doesn’t have the color property. var obj = new Object(“some text”); alert(obj instanceof String); //true 比较不同: var value = “25”; var number = Number(value); //casting function alert(typeof number); //”number” var obj = new Number(value); //constructor alert(typeof obj); //”object” The Boolean Type var booleanObject = new Boolean(true); Boolean对象是通过valueOf()方法返回true或false原始值的。 Instances of Boolean override the valueOf() method to return a primitive value of either true or false. 在Boolean表达式中会引起理解上的混乱,比如: var falseObject = new Boolean(false); var result = falseObject && true; alert(result); //true var falseValue = false; result = falseValue && true; alert(result); //false 还有: alert(typeof falseObject); //object alert(typeof falseValue); //boolean alert(falseObject instanceof Boolean); //true alert(falseValue instanceof Boolean); //false 建议永不使用Boolean对象。 The Number Type var numberObject = new Number(10); toString()方法: var num = 10; alert(num.toString()); //”10” alert(num.toString(2)); //”1010” alert(num.toString(8)); //”12” alert(num.toString(10)); //”10” alert(num.toString(16)); //”a” toFixed()方法: var num = 10; alert(num.toFixed(2)); //”10.00” var num = 10.005; alert(num.toFixed(2)); //”10.01” toExponential()方法: var num = 10; alert(num.toExponential(1)); //”1.0e+1” toPrecision()方法:??科学技术法 var num = 99; alert(num.toPrecision(1)); //”1e+2” alert(num.toPrecision(2)); //”99” alert(num.toPrecision(3)); //”99.0” var numberObject = new Number(10); var numberValue = 10; alert(typeof numberObject); //”object” alert(typeof numberValue); //”number” alert(numberObject instanceof Number); //true alert(numberValue instanceof Number); //false The String Type var stringObject = new String(“hello world”); 只有一个length属性: var stringValue = “hello world”; alert(stringValue.length); //”11” Character Methods var stringValue = “hello world”; alert(stringValue.charAt(1)); //”e” var stringValue = “hello world”; alert(stringValue.charCodeAt(1)); //outputs “101” var stringValue = “hello world”; alert(stringValue[1]); //”e” String-Manipulation Methods var stringValue = “hello “; var result = stringValue.concat(“world”); //concat可以组合任何个数据的字符串参数。 alert(result); //”hello world” alert(stringValue); //”hello” +方法比concat方法在字符串组合运算中效率更好,也运用的更多。 var stringValue = “hello world”; alert(stringValue.slice(3)); //”lo world” alert(stringValue.substring(3)); //”lo world” alert(stringValue.substr(3)); //”lo world” alert(stringValue.slice(3, 7)); //”lo w” alert(stringValue.substring(3,7)); //”lo w” alert(stringValue.substr(3, 7)); //”lo worl” // substr(), the second argument is the number of characters to return. var stringValue = “hello world”; alert(stringValue.slice(-3)); //”rld” alert(stringValue.substring(-3)); //”hello world” alert(stringValue.substr(-3)); //”rld” alert(stringValue.slice(3, -4)); //”lo w” alert(stringValue.substring(3, -4)); //”hel” alert(stringValue.substr(3, -4)); //”” (empty string) String Location Methods var stringValue = “hello world”; alert(stringValue.indexOf(“o”)); //4 alert(stringValue.lastIndexOf(“o”)); //7 var stringValue = “hello world”; alert(stringValue.indexOf(“o”, 6)); //7 alert(stringValue.lastIndexOf(“o”, 6)); //4 查找下列字符串中,所有e的位: var stringValue = “Lorem ipsum dolor sit amet, consectetur adipisicing elit”; var positions = new Array(); var pos = stringValue.indexOf(“e”); while(pos > -1){ positions.push(pos); pos = stringValue.indexOf(“e”, pos + 1); } alert(positions); //”3,24,32,35,52” The trim() Method, trimLeft(),trimRight() var stringValue = “ hello world “; var trimmedStringValue = stringValue.trim(); alert(stringValue); //” hello world “ alert(trimmedStringValue); //”hello world” String Case Methods 有关大小写方法: var stringValue = “hello world”; alert(stringValue.toLocaleUpperCase()); //”HELLO WORLD” alert(stringValue.toUpperCase()); //”HELLO WORLD” alert(stringValue.toLocaleLowerCase()); //”hello world” alert(stringValue.toLowerCase()); //”hello world” String Pattern-Matching Methods 类同RegExp.exec()方法: var text = “cat, bat, sat, fat”; var pattern = /.at/; //same as pattern.exec(text) var matches = text.match(pattern); alert(matches.index); //0 alert(matches[0]); //”cat” alert(pattern.lastIndex); //0 search()方法: var text = “cat, bat, sat, fat”; var pos = text.search(/at/); alert(pos); //1 在replace方法中会用到search逻辑: var text = “cat, bat, sat, fat”; var result = text.replace(“at”, “ond”); alert(result); //”cond, bat, sat, fat” result = text.replace(/at/g, “ond”); alert(result); //”cond, bond, sond, fond” var text = “cat, bat, sat, fat”; result = text.replace(/(.at)/g, “word ($1)”); alert(result); //word (cat), word (bat), word (sat), word (fat) function htmlEscape(text){ return text.replace(/[<>”&]/g, function(match, pos, originalText){ switch(match){ case “<”: return “<”; case “>”: return “>”; case “&”: return “&”; case “\””: return “"”; } }); } alert(htmlEscape(“<p class=\”greeting\”>Hello world!</p>”)); //”<p class="greeting">Hello world!</p>”; split()方法: var colorText = “red,blue,green,yellow”; var colors1 = colorText.split(“,”); //[“red”, “blue”, “green”, “yellow”] var colors2 = colorText.split(“,”, 2); //[“red”, “blue”] var colors3 = colorText.split(/[^\,]+/); //[“”, “,”, “,”, “,”, “”] The localeCompare() Method var stringValue = “yellow”; alert(stringValue.localeCompare(“brick”)); //1 alert(stringValue.localeCompare(“yellow”)); //0 alert(stringValue.localeCompare(“zoo”)); //-1 The fromCharCode() Method alert(String.fromCharCode(104, 101, 108, 108, 111)); //”hello” HTML Methods METHOD OUTPUT anchor(name) <a name=“name”>string</a> big() <big>string</big> bold() <b>string</b> fixed() <tt>string</tt> fontcolor(color) <font color=“color”>string</font> fontsize(size) <font size=“size”>string</font> italics() <i>string</i> link(url) <a href=“url”>string</a> small() <small>string</small> strike() <strike>string</strike> sub() <sub>string</sub> sup() <sup>string</sup>
byhard对本书的所有笔记 · · · · · ·
-
第136页 THE FUNCTION TYPE
js中函数实际上是object. Each function is an instance of the Function type that has prop...
-
第141页 THE FUNCTION TYPE
Function Internals Two special objects exist inside a function: arguments and this. the...
-
第146页 PRIMITIVE WRAPPER TYPES
-
第161页 SINGLETON BUILT-IN OBJECTS
The Global Object URI-Encoding Methods encodeURI() & encodeURIComponent() 比如: v...
-
第165页 SINGLETON BUILT-IN OBJECTS
The Math Object The min() and max() Methods var max = Math.max(3, 54, 32, 16); alert(ma...
说明 · · · · · ·
表示其中内容是对原文的摘抄