JavaScript学习总结(三、函数声明和表达式、this、闭包和引用、arguments对象、函数间传递参数)
一、函数声明和表达式
函数声明: function test() {};
test(); //运行正常
function test() {};
函数表达式: var test = function() {};
test; //undefined
test(); //TypeError
var test = function() {};
命名函数的赋值表达式:
var test = function bar() {
test(); //正常运行
};
test(); //ReferenceError
二、this
1. 全局范围内、函数调用、方法调用、调用构造函数、显示的设置this;
注:在严格模式下,没有全局变量,即this = undefined,在对象的字面量声明语法中,this不能用来指向对象本身;
显示的设置this:
function test(a, b, c) {};
var bar = {};
foo.apply(bar, [1, 2, 3]); //数组将被扩展;
foo.call(bar, 1, 2, 3); //传递到test的参数为: a = 1, b = 2,c = 3;
当使用Function.prototype上的call或apply方法时,函数内的this将被显示设置为函数调用的第一个参数;
2. 常见误解
1. Foo.method = function() {
function test() {
//this 为全局变量;
};
test();
};
Foo.method = function() {
var that = this;
function test() {
//that指向Foo对象;
};
test();
};
三、闭包和引用
闭包:当前作用域总是能访问外部作用域中的变量。
例:
function counter(start) {
var count = start;
return {
increment: function() {
count++;
},
get: function() {
return count;
}
};
};
var test = counter(4);
test.increment();
test.get(); //5;
循环中的闭包:
for (var i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i); // 10个10;
}, 1000);
};
方法:
1. for (var i = 0; i < 10; i++) {
(function(e) {
setTimeout(function() {
console.log(e); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}, 1000);
})(i);
};
2. for (var i = 0; i < 10; i++) {
setTimeout(function(e) {
return function() {
console.log(e); //0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
}(i), 1000);
};
四、arguments对象
每个函数内都可以访问一个变量arguments,它维护着所有传进来的参数列表,不是一个数组,不能用pop()、push()等数组方法,但是能访问其length;
注:当arguments作为局部变量声明和形参时,arguments对象不会被创建;
注:强烈建议不要使用arguments.callee;
转换为数组:Arrary.prototype.slice.call(arguments);但转化比较慢,不咋推荐;
arguments会创建getter和setter方法,改变形参的值会改变arguments对象的值,反之亦然;
例:
function test(a, b, c) {
arguments[0] = 10;
console.log(a); //10;
b = 20;
console.log(arguments[1]); //20;
var d = c;
d = 9;
console.log(c); //3;
};
test(1, 2, 3);
function test(a) {
'use strict';
a = 10;
console.log(a, arguments[0]); //10, 1
};
test(1);
五、函数间传递参数
1. function foo() {
test.apply(null, argument);
};
function(a, b, c) {};
2. 同时使用call和apply:
function Foo() {};
Foo.prototype.method = function(a, b, c) {
console.log(this, a, b, c);
};
Foo.method = function() {
Foo.call.apply(Foo.prototype.method, arguments);
};
或者:
Foo.method = function() {
var args = Array.prototype.slice.call(arguments);
Foo.prototype.method.apply(args[0], args.slice[1]);
};
JavaScript学习总结(三、函数声明和表达式、this、闭包和引用、arguments对象、函数间传递参数)的更多相关文章
- JavaScript学习记录三
title: JavaScript学习记录三 toc: true date: 2018-09-14 23:51:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...
- Javascript 函数声明、调用、闭包
1 # Javascript 函数声明.调用.闭包 2 # 一.函数声明 3 # 1.直接声明.浏览器在执行前,会先将变量和函数声明进行提升. 4 fn(); 5 function fn () { 6 ...
- 浅析匿名函数、lambda表达式、闭包(closure)区别与作用
浅析匿名函数.lambda表达式.闭包(closure)区别与作用 所有的主流编程语言都对函数式编程有支持,比如c++11.python和java中有lambda表达式.lua和JavaScript中 ...
- javascript Arguments对象——函数的实际参数
在javascript函数体内,标识符arguments具有特殊含义.它是调用对象的一个特殊属性,用来引用Arguments对象.Arugments对象就像数组,注意这里只是像并不是哈. javasc ...
- JavaScript学习总结(三)——闭包、IIFE、原型、函数与对象
一.闭包(Closure) 1.1.闭包相关的问题 请在页面中放10个div,每个div中放入字母a-j,当点击每一个div时显示索引号,如第1个div显示0,第10个显示9:方法:找到所有的div, ...
- JavaScript学习笔记(三)——this、原型、javascript面向对象
一.this 在JavaScript中this表示:谁调用它,this就是谁. JavaScript是由对象组成的,一切皆为对象,万物皆为对象.this是一个动态的对象,根据调用的对象不同而发生变化, ...
- JavaScript学习总结(三)——this、原型、javascript面向对象
一.this 在JavaScript中this表示:谁调用它,this就是谁. JavaScript是由对象组成的,一切皆为对象,万物皆为对象.this是一个动态的对象,根据调用的对象不同而发生变化, ...
- JS函数声明与定义,作用域,函数声明与表达式的区别
Scoping & Hoisting 例: var a = 1; function foo() { if (!a) { var a = 2; } alert(a); }; foo(); 上面这 ...
- JavaScript学习第三天
今天学习第三天. 凡事都是需要坚持的,坚持下去. 学习内容: 1.document.getElementById(""),document.getElementByTagName( ...
随机推荐
- Centos7 Tomcat9随机启动
环境: Centos7.JDK 1.8.Tomcat9 安装好JDK跟Tomcat后在/usr/lib/systemd/system/目录下新建文件tomcat.service,内容如下,对应的位置替 ...
- maven项目中的pom.xml
需要配置的内容 1.配置头(自动生成) 2.maven项目的坐标(自动生成) <modelVersion>4.0.0</modelVersion> <groupId> ...
- [转]How rival bots battled their way to poker supremacy
How rival bots battled their way to poker supremacy http://www.nature.com/news/how-rival-bots-battle ...
- 机器学习: 共轭梯度算法(PCG)
今天介绍数值计算和优化方法中非常有效的一种数值解法,共轭梯度法.我们知道,在解大型线性方程组的时候,很少会有一步到位的精确解析解,一般都需要通过迭代来进行逼近,而 PCG 就是这样一种迭代逼近算法. ...
- 四则运算V1.1
作业:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/997 代码:https://coding.net/u/Dawnfox/p/f4 ...
- iptables filter表 案例、iptables nat表的路由功能 、端口映射
1.小案例 #!/bin/bashipt="/usr/sbin/iptables"$ipt -F$ipt -P INPUT DROP$ipt -P OUTPUT ACCEPT$ip ...
- Eclipse maven 错误修正方法:An error occurred while filtering resources
最近打开Eclipse后发现项目报红叉,解决办法如下: 1.eclipse中删除该项目(注意:不要删除代码) 2.cmd,进入到项目目录下,执行命令:mvn eclipse:clean 3.重新导入项 ...
- 使用ionic开发时用遇到监听手机返回按钮的问题~
当时用的是ionic开发一个app,需求是,当按下手机的返回按钮,在指定的页面双击退出,而在其他页面点击一次返回到上个页面: 其实用ionic自带的服务就可以解决: //双击退出 $ionicP ...
- imrersize函数
imrersize函数: 用法:imresize(图像I,method,倍数) 'nearest'(默认值)最近邻插值'bilinear'双线性插值'bicubic'双三次插值 使用方法: clear ...
- 使用patroni 解决hasura graphql-engine pg 数据库ha的问题
环境准备 机器pg 数据库地址修改为haproxy 的ip地址,端口是haproxy的tcp 端口,配置比较简单 hasura graphql-engine docker-compose versio ...