JavaScript应用(一)---基本使用

javascript的lamda表达式:

var created= () => console.log(1)

javascript的函数式编程:

// forEach用于遍历,filter、map、reduce一套操作很有函数式编程的特点
var a=['a','b','c'];
a.forEach((val,index)=>{console.log(index+'--->'+val)})
var c = a.filter((i)=>{console.log(i);return true;})
var c = a.map((i,j)=>{return 'v';})
var c = a.reduce((p1,p2)=>{return p1+p2;});

javascript的原型链:

// object
a={
	say:function(){
		console.log(123)
	}
}
a.__proto__===Object.prototype
// 在a的__proto__属性上添加的方法与属性,可以被直接访问到,相当于在Object.prototype上添加(不推荐这样写,影响太大)
a.__proto__.sayHello=()=>{console.log('hello')}
a.sayHello()
//通过上面的语句,新建的对象也拥有sayHello方法
b={}
b.sayHello()
// function
function c(){}
c.__proto__===Function.prototype
//通常
Function.__proto__===Function.prototype
//基本所有对象(function)的prototype属性是一个对象
//但是Function的prototype属性比较特殊,它也是一个函数,但是继承自对象
Function.prototype.__proto__===Object.prototype
Object.prototype.__proto__===null
//function的prototype包含自己的特殊属性与方法,使用new关键字操作function得到的实例的__proto__属性指向构造function
//的prototype属性,因为实例调用自身__proto__属性上的属性与方法,可以直接调用,因此实例拥有构造function的属性与方法,
//js通过这种原型链的特性可以实现类的功能

javascript中实现面向对象编程的几种方式:

// 使用js实现面向对象
// 1.这种方式可以实现实例化类与继承的功能
function c(){
	this.name="aaa";
	this.say=function(){console.log(123)}
}
function d(){
	var a = new c()
	for(var i in a){
		this[i] = a[i];
	}
	this.say=function(){console.log(111)}
	this.listen = function(){
		console.log('listen');
	}
}

// 2.以下为标准实现
// 由prototype不断地具现化,继承的子类通过prototype属性进行继承,类的实例通过__proto__属性进行向上迭代引用(找到为止)
function c(){}
c.prototype.name="aaa"
c.prototype.say=function(){console.log(123)}
	//继承
function d(){}
d.prototype = new c();
d.prototype.listen = function(){
	console.log('listen')
}
var parentSay = d.prototype.say;
d.prototype.say=function(){		//复写父类的方法
	parentSay()			//调用父类的方法
	console.log('dsay2')
}
e = new d()	//e.__proto__->d.prototype[new c()].__proto__==c.prototype


// 3.这种方式实例化对象时,与通常语言实例化实例的方式不符,这里只是实现一个实例化与继承的功能
function c(){
	var _this={};
	_this.name="aaa";
	_this.sayHello = function(){
		console.log(123)
	}
	return _this;
}
function d(){
	var _this = c();
	_this.age = 12;
	var parentSayHello = _this.sayHello;
	_this.sayHello=function(){	//复写方法
		parentSayHello()	//调用父类的函数
		console.log(111)
	}
	return _this;
}
e = d();

 

参考:

https://www.jikexueyuan.com/course/210.html

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值