一、構(gòu)造函數(shù)
var mrZhang = {
name:"zhangsan",
sex:"男",
age:24,
healthy:100,
smoke:function(){
console.log("I am smoking");
this.healthy --;
},
drink:function(){
console.log("I am drinking");
this.healthy ++;
}
}
//誰觸發(fā)的事件,this就指向誰
//這個(gè)方法屬于誰的,this就指向誰
對(duì)象的創(chuàng)建方法
var obj = {} plainObject 對(duì)象的字面量/對(duì)象直接量
構(gòu)造函數(shù)
系統(tǒng)定義:new Object()
自定義:(大駝峰式寫法:AaaBbbCcc、小駝峰式寫法:aaaBbbCcc)
function AaaBbbCcc(){ //構(gòu)造函數(shù)
}
var a = new abc() //實(shí)例化
function Car(color){
this.color = color;
this.name = "BAOMA";
this.height = 1600;
this.weight = 1000;
this.healthy = 100;
this.run = function(){
this.healthy --;
}
}
var car1 = new Car("red");
var car2 = new Car("black");
二、包裝類
var num = 123;
num.add = 4;
//new Number(num).add = 4; delete
console.log(num.add);
var str = "string";
str.length = 2;
//new String("string").length = 2; delete
console.log(str.length);
var arr = [1, 2, 3, 4, 5, 6];
arr.length = 2;
例子:
var str = "123";
str += 1;
var test = typeof(str); //test == "string";
if(test.length == 6){
test.sign = "typeof的返回結(jié)果可能為String"; //delete
}
console.log(test.sign);
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);
console.log(x, y, z);
//下面的代碼中console.log的結(jié)果是[1, 2, 3, 4, 5]的選項(xiàng)是
//A
function foo(x){
console.log(arguments);
return x;
}
foo(1, 2, 3, 4, 5);
//B
function foo(x){
console.log(arguments);
return x;
}(1, 2, 3, 4, 5)
//C
(function foo(x){
console.log(arguments);
return x;
})(1, 2, 3, 4, 5)
typeof可能返回的結(jié)果是
1.Number
2.String
3.Array
4.Boolean
5.Object
6.function
7.null
8.undefined
124568
//下面alert的結(jié)果是什么
function a(x, y, z){
arguments[2] = 4;
alert(z);
}
a(1, 2, 3);
function a(x, y, z){
z = 4;
alert(arguments[2]);
}
a(1, 2, 3);