Object.prototype和Function.prototype一些经常使用方法

Object.prototype

方法:javascript

  1. hasOwnProperty 概念:用来判断一个对象中的某一个属性是不是本身提供的(主要是判断属性是原型继承仍是本身提供的) 语法:对象.hasOwnProperty('属性名')
 var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } Person.prototype = o; var p = new Person(); console.log( p.hasOwnProperty( 'name' ) ); // false 这个 name 属性不是本身提供的 console.log( p.hasOwnProperty( 'age' ) ); // true 这个 nage 属性是本身提供的 //for in 遍历对象时 有时但愿遍历的是对象本身提供的成员而不是从原型那里继承的 for(var k in p){ if(p.hasOwnProperty(k)){ console.log(k); } }var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } Person.prototype = o; var p = new Person(); console.log( p.hasOwnProperty( 'name' ) ); // false 这个 name 属性不是本身提供的 console.log( p.hasOwnProperty( 'age' ) ); // true 这个 nage 属性是本身提供的 //for in 遍历对象时 有时但愿遍历的是对象本身提供的成员而不是从原型那里继承的 for(var k in p){ if(p.hasOwnProperty(k)){ console.log(k); } }
  1. isPrototypeOf 概念:判断是否是对象的原型 ;前面这个对象是否在后面这个对象的原型链上 语法:对象.isPrototypeOf(对象)
 var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } var p1 = new Person(); Person.prototype = o; var p2 = new Person(); console.log( o.isPrototypeOf( p1 ) ); //false o 不是 p1 的原型 console.log( o.isPrototypeOf( p2 ) );//true o 是 p2 的原型 console.log( Object.prototype.isPrototypeOf( p1 ) );//true Object.prototype 在 p1 的原型链上 console.log( Object.prototype.isPrototypeOf( p2 ) );//true Object.prototype 在 p2 的原型链上var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } var p1 = new Person(); Person.prototype = o; var p2 = new Person(); console.log( o.isPrototypeOf( p1 ) ); //false o 不是 p1 的原型 console.log( o.isPrototypeOf( p2 ) );//true o 是 p2 的原型 console.log( Object.prototype.isPrototypeOf( p1 ) );//true Object.prototype 在 p1 的原型链上 console.log( Object.prototype.isPrototypeOf( p2 ) );//true Object.prototype 在 p2 的原型链上
  1. propertyIsEnumerable 概念:判断对象的某一个属性是否是本身提供的( 与 hasOwnProperty 同样 ), 同时该属性要求可枚举( for-in 遍历出来) 4.补充:(得到对象类型的方法 typeof) toString 用法:Object.prototype.toString.call(对象)->[Object 构造函数名]

Function.prototype

方法: 1.apply call 用法:上下文调用模式,自定义设置this的含义java

语法: 函数名.apply(对象,[参数]) 函数名.call(对象,参数)程序员

描述:数组

  1. 函数名就是表示函数自己,使用函数进行调用的时候默认this 就是全局变量app

  2. 函数名是方法时,this是指当前对象函数

  3. 使用apply调用后,不管是函数仍是方法this都由apply的第一个参数决定ui

注意:this

  1. 若是函数或方法中没有this的操做,不管什么调用都同样spa

  2. 若是是函数调用foo(),有点像foo.apply(window)prototype

  3. 若是是方法调用o.method(),有点像o.method.apply(o)

参数问题:若是函数或方法无参数时,用call或是apply都同样

第一个参数的使用规则:

  1. 若是传入的是一个对象,就是设置该函数或方法中的this指向这个对象
 var func2 = function() { this.name = "程序员"; }; var o={name:'xjj'}; func2.apply(o); console.log(o.name);// 程序员var func2 = function() { this.name = "程序员"; }; var o={name:'xjj'}; func2.apply(o); console.log(o.name);// 程序员
  1. 若是不传参或传入为null undefined等 this默认为window

  2. 若是传入的是基本数据类型,this就指向这个基本类型对应的包装类型的引用

第二个参数:在使用上下文调用时,若是原函数或方法有参数,那么就使用第二个参数来表示

应用:上下文调用只是能修改this,可是最多使用的是借用函数调用

 var a = {}; a[ 0 ] = 'a'; a[ 1 ] = 'b'; a.length = 2; var arr=[]; var newArr=arr.push.apply(arr,a); push 返回所加元素的个数,newArr=2;arr=["a", "b"] a是一个伪数组,没法使用数组的方法, 处理数组转换,能够使用apply将数组或伪数组展开的特性,将元素一个一个取出来构成一个新数组var a = {}; a[ 0 ] = 'a'; a[ 1 ] = 'b'; a.length = 2; var arr=[]; var newArr=arr.push.apply(arr,a); push 返回所加元素的个数,newArr=2;arr=["a", "b"] a是一个伪数组,没法使用数组的方法, 处理数组转换,能够使用apply将数组或伪数组展开的特性,将元素一个一个取出来构成一个新数组

在来一个例子:

 var a = { length: 0 }; a[ a.length++ ] = 'abc'; a[ a.length++ ] = 'def'; var arr=[]; var newArr=arr.slice.apply(a,[0]); 结果为: newArr=["abc", "def"]; slice语法: arr.slice( index, endIndex ) 从index开始,endIndex取不到 返回的是截取到元素 若是第二个参数不传, 那么就是 从 index 一致获取到结尾,该方法不会修改原数组var a = { length: 0 }; a[ a.length++ ] = 'abc'; a[ a.length++ ] = 'def'; var arr=[]; var newArr=arr.slice.apply(a,[0]); 结果为: newArr=["abc", "def"]; slice语法: arr.slice( index, endIndex ) 从index开始,endIndex取不到 返回的是截取到元素 若是第二个参数不传, 那么就是 从 index 一致获取到结尾,该方法不会修改原数组

2.caller 概念:通常不推荐使用. 得到函数的调用者.

3.bind 概念:绑定, 这个语法来源于 ES5

 var f = document.getElementById; f( 'id' ); //这样会报错 非法调用 f.call( document, 'id' ); 或者换一种方式: var f = document.getElementById.bind( document ); f( 'id' ); //这样不会报错var f = document.getElementById; f( 'id' ); //这样会报错 非法调用 f.call( document, 'id' ); 或者换一种方式: var f = document.getElementById.bind( document ); f( 'id' ); //这样不会报错
  1. instanceof 运算符 概念:判断对象是否是由指定构造方法所建立或者说构造函数的原型属性是否在对象所在的原型链上. 语法: 对象 instanceof 函数 描述: '函数.prototype' 是否在 '对象' 的原型链上

     function Person() {} var p = new Person(); console.log( p instanceof Person );//truefunction Person() {} var p = new Person(); console.log( p instanceof Person );//true
     function Person(){}; var p1=new Person(); Person.prototype={}; var p2=new Person(); console.log(p1 instanceof Person); //false Person.prototype不在 p1 的原型链上 console.log(p2 instanceof Person); //true Person.prototype在 p2 的原型链上function Person(){}; var p1=new Person(); Person.prototype={}; var p2=new Person(); console.log(p1 instanceof Person); //false Person.prototype不在 p1 的原型链上 console.log(p2 instanceof Person); //true Person.prototype在 p2 的原型链上
相关文章
相关标签/搜索