/** * 对象的增删改查 */ var obj = { name:"abc", age:40, sex:"male", health : 100, somke:function () { console.log("xxxxxxxxxxa "); this.health-- }, drink:function () { console.log("hhhhhhhhhhh"); this.health++ } } /** * 增加属性,直接增加 */ obj.wife = "none"; /** * 改变属性,直接改变 */ obj.wife = "hahaha"; /** * 删除属性 */ delete obj.name /** * 对象创建方法 */ 1 var obj={} 对象字面量方法 2 构造函数 大驼峰式 TheFirstName() 1)系统自带构造函数 new Object() var obj = new Object() function Car() { this.name = "BMW" ; this.height = "1400"; this.lang = "4900"; this.weight = "1000" } var car =new Car(); var car1 =new Car(); car.name = "奔驰"; car1.name = "布加迪"; //独立 console.log(car) console.log(car1) function Student(name,age,sex) { this.name = name; this.age = age; this.sex = sex } var stydent1 = new Student("BMW","19","MALE") console.log(stydent1) //Student {name: "BMW", age: "19", sex: "MALE"} var num =4; num.len = 3; console.log(num.len) //undefined var str = "abcd"; str.length = 2; console.log(str) //abcd var str = "abc"; str+=1; var test = typeof str; if(test.length == 6){ test.sign = "哈哈哈哈或或或或或或或" } console.log(test.sign) //undefined /** * 例题 */ var a = 5; function test() { a = 0; console.log(a); console.log(this.a); var a; console.log(a) } //test(); // 0 5 0 var temp = new test(); console.log(temp) //0 undefined 0 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; } var oPerson = new Person() oPerson.say(); //1 oPerson.say(); //2 var oPerson1 = new Person(); oPerson1.say(); //1
对象

最新推荐文章于 2024-09-04 16:37:26 发布