面向对象1

一、构造函数

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 ++;
  }
}
//谁触发的事件,this就指向谁
//这个方法属于谁的,this就指向谁

对象的创建方法
var obj = {} plainObject 对象的字面量/对象直接量
构造函数
系统定义:new Object()
自定义:(大驼峰式写法:AaaBbbCcc、小驼峰式写法:aaaBbbCcc)
function AaaBbbCcc(){ //构造函数
}
var a = new abc() //实例化

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的返回结果可能为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的结果是[1, 2, 3, 4, 5]的选项是
//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可能返回的结果是
1.Number
2.String
3.Array
4.Boolean
5.Object
6.function
7.null
8.undefined

124568
//下面alert的结果是什么
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);

你可能感兴趣的:(面向对象1)