一个 javascript 字符串,归根结底,是一个对象。这个对象被分解成一个字符数组。这就是为什么您可以在字符串对象(如数组)上调用一些相同的函数的原因。
任何时候你操作一个字符串字面量,在这种情况下,它都会变成一个字符串对象。字符串对象是一个字符数组。这将允许在使用以下函数时轻松大写:toUpperCase() & toLowerCase() - (以及其他)
this
正如其他人所说,使用被引用为对象。这里有一个小测试代码来展示一个字符串如何像一个数组:
var testStr = "test";
var obj = [ "one", "two", "three"];
console.log(testStr.charAt[1]); // Will return 'e'
console.log(testStr[1]); // Will return 'e'
console.log(obj[1]); // Will return 'two'
console.log(testStr.length) // Will return 4
console.log(obj.length) // Will return 3
在这种情况下,字符串文字var testStr = "string"
是没有函数的原始数据类型。但是字符串对象用于使用函数和非原始数据类型来操作数据。
现在我将创建一个字符串对象并将其输出到控制台:(
当您操作字符串文字时会发生这种情况,它会被转换为字符串对象)
strTest = new String("TEST String");
console.log(strTest);
你会得到:
String
0: "T"
1: "E"
2: "S"
3: "T"
4: " "
5: "S"
6: "t"
7: "r"
8: "i"
9: "n"
10: "g"
length: 11
__proto__: String
anchor: function anchor() { [native code] }
big: function big() { [native code] }
blink: function blink() { [native code] }
bold: function bold() { [native code] }
camelCase: function (){return this.replace(/-\D/g,function(match){return match.charAt(1).toUpperCase();});}
capitalize: function (){return this.replace(/\b[a-z]/g,function(match){return match.toUpperCase();});}
charAt: function charAt() { [native code] }
charCodeAt: function charCodeAt() { [native code] }
checkAllAvailableTags: function (){var b=this,d;for(d in c)c.hasOwnProperty(d)&&(b=b.replace(d,c[d]));return b}
clean: function (){return this.replace(/\s{2,}/g,' ').trim();}
concat: function concat() { [native code] }
constructor: function String() { [native code] }
contains: function (string,s){return(s)?(s+this+s).indexOf(s+string+s)>-1:this.indexOf(string)>-1;}
escapeRegExp: function (){return this.replace(/([.*+?^${}()|[\]\/\\])/g,'\\$1');}
fixed: function fixed() { [native code] }
fontcolor: function fontcolor() { [native code] }
fontsize: function fontsize() { [native code] }
hexToRgb: function (array){var hex=this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);return(hex)?hex.slice(1).hexToRgb(array):false;}
hyphenate: function (){return this.replace(/\w[A-Z]/g,function(match){return(match.charAt(0)+'-'+match.charAt(1).toLowerCase());});}
indexOf: function indexOf() { [native code] }
italics: function italics() { [native code] }
lastIndexOf: function lastIndexOf() { [native code] }
length: 0
link: function link() { [native code] }
localeCompare: function localeCompare() { [native code] }
match: function match() { [native code] }
replace: function replace() { [native code] }
rgbToHex: function (array){var rgb=this.match(/\d{1,3}/g);return(rgb)?rgb.rgbToHex(array):false;}
search: function search() { [native code] }
slice: function slice() { [native code] }
small: function small() { [native code] }
split: function split() { [native code] }
strike: function strike() { [native code] }
sub: function sub() { [native code] }
substr: function substr() { [native code] }
substring: function substring() { [native code] }
sup: function sup() { [native code] }
test: function (regex,params){return(($type(regex)=='string')?new RegExp(regex,params):regex).test(this);}
toFloat: function (){return parseFloat(this);}
toInt: function (){return parseInt(this,10);}
toLocaleLowerCase: function toLocaleLowerCase() { [native code] }
toLocaleUpperCase: function toLocaleUpperCase() { [native code] }
toLowerCase: function toLowerCase() { [native code] }
toString: function toString() { [native code] }
toUpperCase: function toUpperCase() { [native code] }
trim: function trim() { [native code] }
trimLeft: function trimLeft() { [native code] }
trimRight: function trimRight() { [native code] }
valueOf: function valueOf() { [native code] }
__proto__: Object