面向对象

Math随机数

<script type="text/javascript">
	// var num = Math.random();
	// alert(num);//弹出0-1之间的随机数

	var a = 10;
	var b = 20;
	// var num = Math.random()*(b-a)+a;
	// alert(num);//弹出10-20之间的随机数

	var arr = [];
	for(var i=0; i<20; i++){
		// var num = Math.floor(Math.random()*(b-a)+a);//向下取整,10-19
		var num = Math.floor(Math.random()*(b-a + 1)+a);//向下取整,10-20
		
		arr.push(num);//生成一个数就放进数组
	}
	alert(arr);//17,20,20,11,11,19,17,16,10,11,16,11,18,13,13,11,17,14,19,19
</script>

单体创建对象

属性:

var Tom = {
	name:'tom',
	age:18,

方法:

showName:function(){
	alert(this.name);
},
showAge:function(){
	alert(this.age);
}
//调用属性
alert(Tom.name);
alert(Tom.age);

//调用方法
Tom.showName();

工厂模式创建对象

<script type="text/javascript">
	function Person(name,age,job){
		//创建一个空对象
		// var o = new Object();//方式一
		var o = {};//方式二

		o.name = name;
		o.age = age;
		o.job = job;

		o.showName = function(){
			alert(this.name);
		}
		o.showAge = function(){
			alert(this.age);
		}
		o.showJob = function(){
			alert(this.job);
		}

		return o;
	}

	var Tom = Person('tom',18,'程序猿');
	Tom.showJob();

	var Jack = Person('jack',19,'攻城狮');
	Jack.showJob();
</script>

构造函数

new的作用就相当于工厂模式中最开始创建了一个空对象,最后把对象返回

<script type="text/javascript">
	function Person(name,age,job){
		this.name = name;
		this.age = age;
		this.job = job;

		this.showName = function(){
			alert(this.name);
		}
		this.showAge = function(){
			alert(this.age);
		}
		this.showJob = function(){
			alert(this.job);
		}
	}

	//new的作用就相当于工厂模式中最开始创建了一个空对象,最后把对象返回
	var Bob = new Person('bob',18,'产品汪');
	Bob.showJob();

	var Alex = new Person('alex',19,'运营喵');
	Alex.showJob();

	alert(Bob.showName == Alex.showName);//false
</script>

原型模式

<script type="text/javascript">
		function Person(name,age,job){
			this.name = name;
			this.age = age;
			this.job = job;

			Person.prototype.showName = function(){
				alert(this.name);
			}
			Person.prototype.showAge = function(){
				alert(this.age);
			}
			Person.prototype.showJob = function(){
				alert(this.job);
			}
		}

		//先去自己的对象中找showName函数,再去构造函数的原型找
		var Lucy = new Person('lucy',18,'测试鼠');
		//重写自身对象中的方法,不会影响其它对象
		Lucy.showName = function(){
			alert('我的名字是' + this.name);
		}
		Lucy.showName();//我的名字是lucy

		var Lily = new Person('lily',19,'市场鸡');
		Lily.showName();//lily

		alert(Lucy.showName == Lily.showName);//false
	</script>

call和apply

<script type="text/javascript">
		/*
		call和apply的区别

		二者都可以改变当前的this,区别在于apply方法要将参数放入数组中再传参
		*/
		function aa(a,b){
			alert('我的this是' + this + ',我的a是' + a + ',我的b是' + b);
		}

		//我的this是[object Window],我的a是2,我的b是3
		// aa(2,3);

		//我的this是abc,我的a是2,我的b是3
		// aa.call('abc',2,3);

		//我的this是abc,我的a是2,我的b是3
		aa.apply('abc', [2,3]);
	</script>

函数的继承

<script type="text/javascript">
		//父类
		function Fclass(name, age){
			this.name = name;
			this.age = age;
		}
		Fclass.prototype.showName = function(){
			alert(this.name);
		}
		Fclass.prototype.showAge = function(){
			alert(this.age);
		}

		//子类
		function Sclass(name, age, job){
			//属性用call或者apply的方式来继承
			Fclass.call(this, name, age);
			this.job = job;
		}
		//方法继承:将父类的一个实例赋值给子类的原型属性
		Sclass.prototype = new Fclass();
		Sclass.prototype.showJob = function(){
			alert(this.job);
		}

		//由于已经继承了父类的属性和方法,所以可以直接调用
		var Driver = new Sclass('tom',18,'老司机');
		Driver.showName();
		Driver.showAge();
		Driver.showJob();
	</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值