对象,包装类

对象
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//第二个函数在预编译时会覆盖第一个
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值