特征: 简单的函数调用, 函数名前面没有任何引导内容数组
function foo(){} var fn = function(){}; ... foo(); fn(); (function(){})(); // 上面的三种都是简单的函数调用
this的含义浏览器
特征: 方法必定是依附于一个对象, 将函数赋值给对象的一个属性, 那么就成为方法.就是函数前面必须有引导对象app
function foo(){ this.method = function(){}; } var o = { method : function(){} }
this的含义函数
注意点
在 arguments 这种伪数组, 或者 [] 数组这样的对象中, 这样调用函数也是方法调用, this 会只指向对象this
分析:
因为构造函数只是给 this 添加成员, 而方法也能够完成这个操做,对与 this 来讲, 构造函数和方法没有本质区别prototype
普通状况, 能够理解为构造函数已经默认进行了 return this, 添加在后面的都不会执行code
特殊状况, return 对象, 最终返回对象对象
,
进行隔离不管是 call 仍是 apply 在没有后面参数的状况下(函数无参数, 方法无参数), 二者一致继承
function foo(){ console.log(this); // this => window } var obj = {}; foo.apply( obj ); // this => obj foo.call( obj ); // this => obj
foo(); foo.apply(); foo.apply(null); foo.apply(undefined); foo.call(); foo.call(null); foo.call(undefined); // 上面都this都指向window
再使用上下文调用的时候, 原函数(方法)可能会带有参数, 那么要让这些参数在上下文中调用, 就须要这个第二, ( n )个参数来表示事件
function foo(num){ console.log(num); } foo.apply(null, [123]); // 至关于 foo(123);
上下文调用只是修改this, 可是使用最多的地方是借用函数调用
var a = {}; a[0] = 'a'; a[1] = 'b'; a.length = 2; // 使用数组自带方法 concat(); // 不修改原数组 var arr = []; var newArr = arr.concat(a);
分析
因为 a 是伪数组, 并非真正的数组, 不能使用数组的方法, concat 会将 a 做为一个总体 Object 加入数组
apply 方法有一个特性, 能够将数组或者伪数组做为参数
foo.apply( obj, 伪数组 ); // IE8 不支持
将 a 做为 apply 的第二个参数
var arr = []; var newArr = Array.prototype.concat.apply( arr, a);
由上面的数组转换, 咱们能够获得结论, 应该涉及到数组操做的方法理论上都应该能够
push, pop, unshift, shift
slice
splice
var a = {length : 0}; // 设置伪数组的长度 a[ a.length++ ] = 'a'; a[ a.length++ ] = 'b'; // 在给伪数组的元素赋值时, 同时修改伪数组的 length 属性 // a[0] = 'a'; a.length++;
var arr = []; arr.push.apply( arr, a ); // 利用 apply 能够处理伪数组的特性, 进行传参 // 至关于 arr.push(a[0], a[1])
slice 方法不会修改原数组
经过 apply 实现伪数组使用 slice 方法
var a = { length : 0 }; a[a.length++] = 'a'; a[a.length++] = 'b'; var arr =[]; var newArr = arr.slice.apply(a ,[0])
传统方法
var arr = [1,2,3,4,5,6,7,8,9]; var max = arr[0]; for(var i = 0;i < arr.length;i++){ if(max < arr[i]){ max = arr[i] } }
使用 apply 借用 Math.max 获取数组中最大值
利用 apply 能够传入参数能够是数组或是伪数组的特性
var arr = [1,2,3,4,5,6,7,8,9]; Math.max.apply(null, arr);
了解了四种函数调用模式, 咱们能够深一步了解建立对象的几种方式, 对象是经过构造函数建立的
document.createElement()
function createPerson(name, age, gender){ var o = {}; o.name = name; o.age = age; o.gender = gender; return o; }
function Person(name,age,gender){ this.name = name; this.age = age; this.gender = gender; }
function createPerson(name,age,gender){ var o = {}; o.name = name; o.age = age; o.gender = gender; return o; } var p = new createPerson('Bob',19,'male')
function createPerson(name,age,gender){ this.name = name; this.age = age; this.gender = gender; } createPerson.prototype = { constructor : createPerson, wife : '高圆圆' }
function Person(name,age,gender){ this.name = name; this.age = age; this.gender = gender; } function Studner(name,age,gender,course){ // 借用构造函数Person, 建立 Student 对象 Person.call(this,name,age,gender); this.course = course; } var boy = new Student('Bob',19,'male',;Math);
// 临时中转 function person(o){ function Foo(){}; F.prototype = o; return new F(); } // 寄生函数 function create(o){ var fn = person(o); fn.move = function(){}; fn.eat = function(){}; fn.sleep = function(){}; return fn; } var boy = { name : 'Bob', age : 19, famliy : ['father','mother','wife'] } var boy1 = create(boy); console.log(boy1.name); console.log(boy1.family); // 此时 boy1 有了 boy 的成员
例题 1
function Foo(){ getName = function(){ alert(1); }; return this; } function getName(){ alert(5); } Foo.getName = function(){ alert(2); }; Foo.prototype.getName = function(){ alert(3); }; getName = function(){ alert(4); }; Foo.getName(); // 2 getName(); // 4 Foo().getName(); // 4 1 getName(); // 4 1 new Foo.getName(); // 2 new Foo().getName(); // 3 new new Foo().getName(); // 3
分析
例题 2
var length = 10; function fn() { console.log( this.length ); } var obj = { length: 5, method: function ( fn ) { fn(); arguments[ 0 ](); // [ fn, 1, 2, 3, length: 4] } }; obj.method( fn, 1, 2, 3 );
分析
例题 3
var o = { method : function(){ console.log(this); } }; o.method(); var f = o.method; f(); (o.method)(); var obj = {}; (obj.fn = o.method)(); (obj.fn)();
分析