JavaScript函数

JavaScript函数


声明函数

	// 函数声名式 特点,变量提升,在同级作用域无论哪里都可以调用
	funtion func1(){
		console.log('函数声名式')
	}
	// 函数表达式 特点:需要先赋值,再调用
	var func1 = function(){
		console.log('函数表达式'
	}
	// 构造函数 基本不会使用,可读性差
	var func2 = new Function('a','b','return a + b')
	

函数的三大特性

	封装:将多个表达式整合成一个函数,从而可以进行反复利用,减少重复代码。并形成一个局部作用域,声名的变量只能在函数中使用,只暴露出调用函数的接口。
	function Cat(){
		var say = '喵'
		this.toSay = function (){
			console.log(say)
		}
	}
	console.log(say) // undefined
	console.log(new Cat().toSay()) // '喵'
	
	继承:利用原型,让函数挂载到另一个函数的原型上,使函数可以使用父级函数的属性和方法。
	function TheName(){
		this.getName = function (){
			console.log(this.name)
		}
	}
	function Cat(){
		this.name = '猫'
	}
	function Dog(){
		this.name = '狗'
	}	
	Cat.prototype =  new TheName();
	Dog.prototype =  new TheName();
	var a = new Cat(),
		b = new Dog()
	a.getName() // 猫
	b.getName() // 猫
	
	多态:一个引用在不同情况下的多种状态。
	function Dog(){
	    this.name = '狗'
	    this.say = '汪汪汪'
	    this.toSay = function () {
	        console.log(this.name + '在' + this.say + '地叫')
	    }
	}
	function Cat(){
	    this.name = '猫'
	    this.say = '喵喵喵'
	}
	var dog = new Dog()
	Cat.prototype = dog
	var cat = new Cat()
	dog.toSay() // 狗在汪汪汪地叫
	cat.toSay() // 猫在喵喵喵地叫

函数实例化

	函数的实例化可以说是创建一个模板,通过创建的模板生成需要的数据
	function Student(name,age,grade){
		this.name = name
		this.age = age
		this.grade = grade
	}
	var student = new Student('小不点',14,'六年级')

this、argument、return

	this
		全局:this指向window
		函数:this指向函数的实例
	
	改变this的几个方法 call,apply,bind
	1)、call
	function a (){
	    this.func = function (e){
	        console.log(this)
	        console.log(e)
	    }
	}
	var b = new a()
	b.func(1) //  a {func: ƒ} 1
	b.func.call(this,1) // window 1 第一个参数为指向的对象 第二个参数以及后面的参数为入参
	
	2)、apply
	function a (){
	    this.func = function (one,two){
	        console.log(this)
	        console.log(one,two)
	    }
	}
	var b = new a()
	b.func(1,2) //  a {func: ƒ}
	b.func.apply(this,[1,2]) // window 1 2 第一个参数为指向的对象 第二个参数为入参,必须是一个数组
	
	3)、bind
	function a (){
	    this.func = function (one,two){
	        console.log(this)
	        console.log(one,two)
	    }
	}
	var b = new a()
	b.func(1,2) //  a {func: ƒ}
	b.func.bind(this)(1,2) // window 1 2 绑定后不会调用,需要添加(),才能调用

	相同点:都可以改变this指向
	不同点:call和apply都是改变this后马上调用,bind需要手动调用。apply的入参是一个数组。

	arguments 类数组、 return 返回值
	function func(a,b){
    	console.log(arguments) // Arguments(2) [1, 2, callee: ƒ, Symbol(Symbol.iterator): ƒ]
    	var arr = []
    	for(var i = 0;i < arguments.length;i++){
			arr.push(arguments[i])
		}
		return arr
	}
	var list = func(1,2) // [1,2]

箭头函数

	特点:
		1、箭头函数中,thsi永远指向他的父级
			function func () {
			    let a = () => {
			        console.log('a',this) // a func {}
			    }
			    function b (){
			         console.log('b',this) //  b window
			    }
			    a()
			    b()
			} 
			let c = new func()
		2、无法改变this 的指向
		3、箭头函数种没有 arguments
		4、箭头函数都是匿名函数,只能通过函数表达式来声明
		5、箭头函数可以省略小括号、大括号和return
			let a = b => console.log(b)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值