对象的包装类
一、首先拋出疑問:
原始值為啥添加屬性而且不會報錯 ???
var str = 'a'; str.name = 'abc'; console.log(str.name); //就是因為原始值要經過包裝類
var str = 'abc'; str.length = 2; // new String('abc').length = 2; delete console.log(str); // abc // new String('abc').length --> 這個 length 方法,是字符串對象自帶的屬性 console.log(str.length); // 3 --> 這是 new String('abc') 的結果var num = 123; num.name = 'abc'; console.log(num.name); // undefined /*new Number(num).name = 'abc' --> deletenew Number(num).name, 此時結果為 */小案例
var str = 'abc'; str += 1; var test = typeof(str); // test == 'string' if (test.length === 6) {test.sign = 'typeof 的返回結果可能為 String ';// new String(test).sign = 'xxx'; ---> 然而字符串對象沒有這個方法 }console.log(test.sign); // undefined ------------------------------------------------------function Person(name, age, sex) {var a = 0;this.name = name;this.age = age;this.sex = sex;function sss() {a ++;console.log(a);}this.say = sss; // 如果誰調用了 say , 那么 say 就會變成一個閉包被返回出去, 而變量 a 就會成為 sss 的私有化變量 }var person = new Person(); person.say(); // 1 person.say(); // 2 var person1 = new Person(); person1.say(); // 1 person1.say(); // 2 var x = 1, y = z = 0;function add(n) {return n = n + 1; }y = add(x);function add(n) { return n = n + 3; }z = add(x);// x : 1 // y : 4 --> 因為函數名如果重復了,就會以最后一個為準 // z : 4包裝類有哪些???
- new Number()
- new String()
- new Boolean()
總結
- 上一篇: LIST_VIEW_和LVITEM用法
- 下一篇: LVITEM结构-列表视图控件的一个数据