call()/apply()/bind()/FD/FE/无函数重载/基本值/引用值
来源:互联网 发布:康佳lc32es62数据 编辑:程序博客网 时间:2024/06/04 20:14
对我来说,博客首先是一种知识管理工具,其次才是传播工具。我的技术文章,主要用来整理我还不懂的知识。我只写那些我还没有完全掌握的东西,那些我精通的东西,往往没有动力写。炫耀从来不是我的本意,好奇才是。 —– 阮一峰
1.from:http://www.cnblogs.com/pingchuanxin/p/5722139.html
function test(){ alert(''+this);}function test2(a){ console.log('this: '+ this); console.log('a: '+ a);}test2.apply(test,[1]);/*this: function test(){ alert(''+this);}a: 1*/
var displayArgs1 = function(val1,val2,val3,val4){ console.log(val1 + ' ' + val2 + ' ' + val3 + ' ' + val4);}var emptyObject = {};var displayArgs2 = displayArgs1.bind(emptyObject,12,'a');displayArgs2(24,'b');//2 12 a 24 b
2.理解JS中的call()/apply()/bind()
【from】http://www.cnblogs.com/libin-1/p/6069031.html
function eat(x,y){ console.log(x+y);}function drink(x,y){ console.log(x-y);}//等价于eat(3,2)。JS中的函数其实是对象,函数名是对Function对象的引用eat.call(drink,3,2);//5
3.浅析JS中对函数function的理解(基础篇)
【from】http://www.jb51.net/article/94773.htm
①函数无重载概念
function sum(num1,num2){ return num1+num2;}function sum(num1,num2,sum3){ return num1+num2+sum3;}//函数名是指向函数对象的一个指针,因此,这个名字只不过是标识了该函数,与参数列表并没有关系。sum(1,2,3);//6//换种更为直观的表示方法var sum = function(num1,num2){ return num1+num2;};sum = function(num1,num2,num3){ return num1+num2+num3;};sum(1,2);//NaNsum(1,2,3);//6
②函数声明和函数表达式
*区别:函数声明有个函数声明提升的过程。
console.log(sum(1,2));function sum(num1,num2){ return num1+num2;}//3console.log(sum2(3,4));var sum2= function(num1,num2){ return num1+num2;};//Uncaught TypeError: sum2 is not a function
4.JS中的变量可以存放两种类型的值,一种为原始值(primitive value),如Undefined,Null,Boolean,Number,String。这类值存放在栈内部,每赋值一次就创建一个新的拷贝。另一种值为引用值(reference value),这类值存放在堆内存中,只能通过引用赋值。
var a = 'test';var b = function() {};b.a = 'test';function change(m,n){ m = 'change'; n.a = 'change';}change(a,b);//基本值----坚如磐石,不为所动//引用值----摇摆不定,墙头小草console.log(b.a);//change
5.闭包
for(var i=0;i<10;i++){ function f1(){ consoloe.log(i);//10 }}f1();//因为i属于全局变量Global,等到输出i时,循环早已完事了//为了输出期望的东西:0,1,2,3,4,5,6,7,8,9//①错的先写出来for(var i=0;i<10;i++){ function f1(){ consoloe.log(i); }}f1();//②花架子搭好---"利用匿名函数人造一个"块级"作用域",传入全局变量i,在循环情况下,分别将0到9传给参数efor(var i=0;i<10;i++){ (function(e){ })(i) function f1(){ consoloe.log(i); }}f1();//③将原来那个函数"塞"进匿名函数,注意i要变成e喔for(var i=0;i<10;i++){ (function(e){ function f1(){ consoloe.log(e); } })(i)}f1();//④将f1函数的调用放入for循环内部for(var i=0;i<10;i++){ (function(e){ function f1(){ consoloe.log(e); } })(i) f1();}//然后就可以运行勒0123456789//let版for(let i=0;i<3;i++){(function f1(e){console.log(e);})(i);}/*012*/
var x = 10;foo ={ x : 20, bar : function(){ var x = 30; return this.x; }};console.log(foo.bar);/*ƒ (){ var x = 30; return this.x; }*/var x = 10;foo ={ x : 20, bar : function(){ var x = 30; return this.x; }};foo.bar = foo.bar;console.log(foo.bar);/*ƒ (){ var x = 30; return this.x; }*/var x = 10;foo ={ x : 20, bar : function(){ var x = 30; return this.x; }};(foo.bar = foo.bar)();//10var x = 10;foo ={ x : 20, bar : function(){ var x = 30; return this.x; }};//使得b指向函数名为bar的函数对象var b = foo.bar;//因此该调用方式为纯粹的函数调用b();//10var x = 10;foo ={ x : 20, bar : function(){ var x = 30; return this.x; }};//该调用方式为作为对象的方法调用(foo.bar)();//20var x = 10;foo ={ x : 20, bar : function(){ var x = 30; return this.x; }};//逗号运算符,结果为最后一项,经过运算后,为纯粹的函数调用方式(foo.bar,foo.bar)();//10
6.几个小例子
var array1 = ['yyc',21];var array2 = ['will','change the world.'];Array.prototype.push.apply(array1,array2);//4array1;//(4) ["yyc", 21, "will", "change the world."]var array1 = ['yyc',21];var array2 = ['will','change the world.'];var link = array1.concat(array2);link;//(4) ["yyc", 21, "will", "change the world."]var number = [2,821,56,72];var max = Math.max.apply(Math,number);max;//821var max = Math.max.call(Math,2,851,56,72);max;//851
阅读全文
0 0
- call()/apply()/bind()/FD/FE/无函数重载/基本值/引用值
- javascript 的bind/apply/call函数
- Javascript中bind、call、apply函数用法
- Javascript中bind、call、apply函数用法
- call(), apply(), bind()三个晦涩函数
- JavaScript之call() 、apply() 、bind()函数
- 关于call(),apply(),bind()函数的理解
- js中call、apply、bind函数
- Javascript中的this,call,apply,bind函数
- call apply 与 bind
- call()、apply()、bind()
- apply bind call区别
- call,apply,bind
- 【JavaScript】call, apply, bind
- bind apply call异同
- apply call bind区别
- bind , call and apply
- JS call bind apply
- 求ax^2+bx+c=0的根
- 笨方法学Python 习题 39: 字典, 可爱的字典
- 斐波那契数列
- UVa10618 Tango Tango Insurrection
- jvm(3)类加载器
- call()/apply()/bind()/FD/FE/无函数重载/基本值/引用值
- pip 相关命令的应用
- MySQL 存储过程参数用法 in, out, inout
- Centos 通过 Nginx 和 vsftpd 构建图片服务器
- API学习Random类随机数生成
- Biridian Forest CodeForces
- ActiveMQ在商城案例中的使用
- Balanced Number [数位DP]
- Word里面的大括号怎么打出来?