<script type="text/javascript">
//构造函数中的this
/*
GO={
Car:(function)
//return this;
car1:{
color:black,
brand:BENz
}
}
执行后
AO:{
this:{
color:color,
brand:brand
}
}
*/
function Car(color,brand){
// this:{
// color:color,
// brand:brand
// }
this.color=color;
this.brand=brand;
//return this; //形成闭包 抛出去
// 这里是隐式的返回this 到全局去 如果你return '什么' 系统会默认return this 如果是返回引用值[] {} 会让函数变为这个引用
}
var car1=new Car('black','Benz');//这里的new是为了实例化 造出一个this 本来this指向window new后于是this指向了car1
console.log("*******************************")
//包装类
//new Number new String new boolean
var a=1;//原始值
console.log(a);
var aa=new Number(1);//对象
console.log(aa);
aa.name='添加属性';
console.log(aa);
var bb=aa+1;//运算时重新回到原始值
console.log(bb);
console.log(aa);
//补充:undefined null 不可以设置方法和属性
//js 包装类
var a=123;//*原始值(没有方法和属性)
a.add=3;
/*
包装类过程:
系统检测后发现错误 为了正常运行 执行a.add=3; 语句后帮你 new Number(123).add=3;
然后系统不能帮你声明变量 所以保存不了值 系统会delete a.add=3 -> new Number(123).add=3
于是打印undefined
*/
//console.log(a.add);//打印undefined
var str1='qwe';
console.log(str1.length);//是因为包装类 系统是这样访问的 console.log(new String(str).length); 不需要保存 才成功执行
//数组的截断方法
var arr2=[1,2,3,4];
arr2.length=3;//arr.length 不仅可以赋值 可以截断数组长度
console.log(arr2);//arr[1,2,3]
var str3 = 'qwe';
str3.length=1; //new String(str).length =1; 因为无法保存值 系统delete 该语句
console.log(str3.length);//3 访问方式: console.log(new String(str3).length)
console.log("------------------------");
function x(brand,color){
//值已经写死
this.brand='B';//B
this.color='R';//R
this.brand=brand;//A
this.color=color;//S
}
var y=new x('A','S');
console.log(y);
</script>
构造函数中的this js包装类
最新推荐文章于 2024-07-30 22:36:34 发布