8.2 js第七天

call apply和bind

我看了下左边有错  打印的第一个是 object

 这个也会在调用时候执行函数

bind 是对比call好点  返回新函数,是不执行的函数,后面跟的参数不是数组或者类数组。

bind的重写

 Function.prototype.bind = function(context){
	        var arg = arguments;
	        var _this = this;
	        return function(){
		    _this.apply(context, arg);
	    }

 检测数据类型方法 

 var obj ={}
        console.log(obj.toString())//[object Object]  这俩后面是构造函数 
        console.log( Object.prototype.toString.call([]))//[object Array]

 call的补充

指定函数this指向  把指定函数执行 

fn.call.call.call(fn1) 执行的是call里面的this  fn1 

Function.prototype.call(fn2,'***')

Function.prototype.call.call.call(fn2)  

小括号中出现多项 (每一项用逗号隔开) 操作只有最后一项 

(1,2,4,3)  //3

console.log(1,2,3,4)打印1 2 3 4

console.log((1,2,3,4))//打印4

获取平均数  1 先排序 2 去掉最大值最小值 3  然后算均值

function fn(){
       var arr= [].slice.call(arguments,0);
       var arr1=arr.sort(function(a,b){return a-b}).slice(1,arguments.length-1);
//之所以要再定义一个function(a,b),根据返回的正负来判断数组中元素的位置,是因为js底层中,数组元素的比较是根据ASCII码值来比较的
       var result = arr1.reduce(function(prev,next){ 
           return prev+next;}
       ,0)
       console.log(arr);
       console.log(result/arr1.length);
    }
    fn(4,2,7,8,12,32)

【那个function的作用就是比较两个数的大小用的,然后返回结果的正负作为排序的依据.

这个函数是升序排序,如果想逆序排序改成return b-a;就行了.
它的排序原理是每2个数比较,然后根据正负更改数组内元素的位置.】【突然发现自己看过的数组全忘了  一点点印象都没了  必须得复习】

浏览器异常信息捕获:

try{//try语句报错才会执行catch语句的内容   
    alert(num)
}catch(e){

}

console.log(e)    e里是报错信息  必须写e  

//将类数组转换为数组

function fn(){
    console.log([].slice.call(arguments,0))
}
fn(1,3,5)

//判断属性是否属于对象     in判断的话不管公有还是私有属性只要有都会返回true

var obj = {age:0.3}
console.log('age'  in obj)//打印true
console.log("toString" in obj)  //往原型上找  打印true 

console.log(obj.hasOwnProperty('toString'))   //打印false

hasOwnPrperty只检测私有属性的   

 

JS继承

1 call apply继承

  function Parent(){
      this.name ='1111'
  }
  Parent.prototype.getName = function(){}
  function Children(){
      
      Parent.call(this);
    //   var obj = {};
    //   Parent.call(obj);
    //   console.log(obj);
      
  }
 var c1 = new Children;
 console.log(c1)

2 原型继承

  function Parent(){
      this.name ='1111'
  }
  Parent.prototype.getName = function(){}
  function Children(){
      
  }
  var p1 = new Parent;
  Children.prototype = p1 ;//这个   继承name和getName方法

 var c1 = new Children;
 console.log(c1)

 

 

3 寄生式组合继承

 function Parent(){
  }
  Parent.prototype.getName = function(){}
  function Children(){
      
  }
  var obj =Object.create(Parent.prototype)
  console.log(obj)
  Children.prototype=obj
  var c1 = new Children
  console.log(c1)

4 对象继承

function Parent(){
  }
  Parent.prototype.getName = function(){}
  function Children(){
      
  }
  var obj =Object.create(Parent.prototype)
 var c1 = new Children;
 c1.__proto__= obj;
 console.log(c1);

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值