對象,包裝類

對象
1.用已學的知識點,描述一下你心目中的對象。
2.屬性的增、刪、改、查
3.對象的創建方法
  • 字面量
  • 構造函數
  1. 系統自帶 new Object();Array();Number();Boolean();String();Date();
  2. 自定義
  • Object.create(原型)方法
var mrDeng = {
	name : "MrDeng",
	age : 40,
	sex : "male",
	health : 100,
	smoke : function () {
		console.log('I am smoking ! cool!!!');
		this.health --;
	},
	drink : function () {
		console.log('I am drink');
		this.health ++;
	} ,
}
mrDeng.smoke();
console.log(mrDeng.health);//99
mrDeng.drink();
console.log(mrDeng.health);//100
//對象裏的方法調用
//this指代第一人稱,指代mrDeng
//屬性的增加
mrDeng.wife = "xiaoliu";

//查看屬性
console.log(mrDeng.health);

//改變屬性值
mrDeng.sex = "female";

//刪除屬性
delete mrDeng.name;
//當一個對象的屬性沒有時被訪問,打印undefined,不會報錯
var deng = {
	prepareWife : "xiaowang",
	name : "laodeng",
	sex : "male",
	gf : "xiaoliu",
	wife : "",
	divorce : function () {
		delete this.wife;
		this.gf = this.prepareWife;
	},
	getmarried : function () {
		this.wife = this.gf;
	} ,
	changePrepareWife : function (someone) {
		this.prepareWife = this.someone;
	}
}
//對象的創建方法
//1.var obj = {}  plainObject 對象字面量/對象直接量
//2.構造函數
//		1)系統自帶的構造函數 Object()
//		2)自定義

var obj = new Object();
obj.name = 'abc';
obj.sex = 'female';
obj.say = function () {};
//外部定義屬性
//構造函數,結構上和函數沒有區別,命名上用大駝峯命名規則 TheFiestName
function Person() {

}
var person = new Person();
function Car(color) {
	this.color = color
	this.name = "BMW";
	this.height = "1400";
	this.lang = "4900";
	this.weight = 1000;
	this.health = 100;
	this.run = function () {
		this.health --;
	}
}
var car = new Car('red');//自定義顏色
var car1 = new Car('green');//構造了2個獨立空間
console.log(car.run());
console.log(car1.health());//100
function Student(name, age, sex) {
	//var this = {
	//	name : "";
	//	age : "";
	//}
	this.name = name;
	this.age = age;
	this.sex = sex;
	this.grade = 2018;
	//return this;
}
var student = new Student('zhangsan', '18' ,'male');
//每一個new的不一樣的值用參數傳進去

構造函數內部原理//有new之後

1.在函數體最前面隱式的加上this = {}
2.執行 this.xxx = xxx;

3.隱式的返回this

function Person(name, height) {
	// var this = {}
	this.name = name;
	this.height = height;
	this.say = function () {
		console.log(this.say);
	}
	//return this;
}
console.log(new Person('xiaowang', 180).name);
function Person(name, height) {
	var that = {};
	that.name = name;
	that.height = height;
	return that;
}
var person = Person('xiaowang', 180);
var person1 = Person('xiaozhang', 175);
//模擬構造函數,可以執行,但是不要這麼用
//有new了不能返回原始值,顯示return 123;不管用,原始值不能有屬性
//var num = new Number(123);數字123是對象
//數字的對象參與運算後變成原始值num*2;246變成原始值
//字符串,布爾類型用法一致
//var str = new String('abc');字符串對象
//var bol = new Boolean('true');布爾對象
//undefined和null不能有屬性


包裝類

var num = 4;
num.len = 3;
//new Number(4).len = 3;	delete
//
//new Number(4).len//undefined,len被刪除沒這個屬性了
console.log(num.len);
var str = "abcd";
str.length = 2;
//new String('abcd').length = 2;	delete//刪的是new的length
//
//new String('abcd').length//4,字符串自帶length
console.log(str);//abcd
var str = "abc";
str += 1;
var test = typeof(str);//test == "string"
if(test.length == 6) {
	test.sign = "typeof的返回結果可能爲String";
	//new String(test).sign = 'xxx';
	//
	//new String(test).sign
}
console.log(test.sign);//undefined

練習

1.

function Person(name, age, sex) {
	var a = 0;
	this.name = name;
	this.age = age;
	this.sex = sex;
	function sss() {
		a ++;
		document.write(a);
	}
	this.say = sss;//閉包
}
var oPerson = new Person();
oPerson.say();//1
oPerson.say();//2
var oPerson1 = new Person();
oPerson1.say();//1

2.下面這段js代碼執行完畢後 x,y,z 的值分別是多少?

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//第二個函數在預編譯時會覆蓋第一個
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章