惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
Latest news
Latest news
Google Online Security Blog
Google Online Security Blog
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
Forbes - Security
Forbes - Security
T
Tenable Blog
The Last Watchdog
The Last Watchdog
T
Tor Project blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
O
OpenAI News
L
LINUX DO - 热门话题
P
Privacy International News Feed
月光博客
月光博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
量子位
博客园 - 【当耐特】
罗磊的独立博客
T
Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Cisco Blogs
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
N
News | PayPal Newsroom
腾讯CDC
Security Latest
Security Latest
J
Java Code Geeks
L
LINUX DO - 最新话题
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Exploit Database - CXSecurity.com
L
Lohrmann on Cybersecurity
D
Docker
Spread Privacy
Spread Privacy
S
Security @ Cisco Blogs
A
Arctic Wolf
H
Hacker News: Front Page
Help Net Security
Help Net Security
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术

博客园 - bigwhiteshark(云飞扬)

linux下svn定时更新项目 PHP合成图片、生成文字、居中对齐、画线、矩形、三角形、多边形、图片抗锯齿、不失真 高性能源码示例 Html5 Canvas transform setTransform uv纹理坐标设定与贴图规则 JavaScript严谨模式(Strict Mode) property和attribute的区别 深入理解javascript 中的 delete(转) javascript中的call()和apply()方法的使用 Javascript 异步加载详解(转) Javascript框架的自定义事件(转) array2json() - Convert PHP arrays to JSON 一些Vim使用的小技巧 virtualbox centos安装增强工具和Centos与VirtualBox共享文件夹设置 yum搭建lnmp环境(CentOS6.4) CentOS中文man安装配置 在Linux里设置环境变量的方法(export PATH) 更新CentOS防火墙设置开启80端口访问 centos 6.4 server 安装nginx Centos安装vim
容易答错的JS笔试题
bigwhiteshark(云飞扬) · 2014-08-10 · via 博客园 - bigwhiteshark(云飞扬)

1,考察this

var length = 10
function fn(){
    alert(this.length)
}
var obj = {
    length: 5,
    method: function(fn) {
        fn() // ?
        arguments[0]() // ?
    }
}
obj.method(fn)

这里的坑主要是arguments,我们知道取对象属于除了点操作符还可以用中括号,这里fn的scope是arguments,即fn内的this===arguments,调用时仅传了一个参数fn,因此length为1。arguments[0] 就是fn,但是直接执行的时候,this指向的是arguments,因为arguments是作为一个调用fn方法的调用者存在,就像a.b(),b里的this指向了a一样。

2,函数表达式具名(函数声明同时赋值给另一个变量)或函数声明立即执行时,名仅在该函数内可访问

~function() {
    alert(typeof next) // ?
    ~function next() {
        alert(typeof next) // ?
    }()
}()

外层匿名函数自执行,打印next,接着内层具名函数自执行。会发现具名的next仅在其自身函数体内可访问,即输出为function。外面是不可见的,typeof就为undefined了。(注:此题IE6/7/8中输出为function function, 标准浏览器为undefined function

同样的情况也发生在将具名函数赋值给一个变量时,如下

var func = function a() {
    alert(typeof a)
}
func() // ?
alert(typeof a) // ?

这条规则是标准中(ES3/ES5)都已明确指出,但IE6、7、8没有严格遵从。可参见w3help的分析或李松峰老师的翻译《命名函数表达式探秘

3,给基本类型数据添加属性,不报错,但取值时是undefined

a = 3
a.prop = 4
alert(a + a.prop) // ?

变量a为数字3,给其添加prop属性,值为4(奇怪吧在JS中这是允许的,且不会有语法错误)。然后alert出a+a.prop的结果。结果是NaN。a.prop为undefined,3+undefined为NAN。

举一反三,给字符串添加属性

str = 'a'
str.prop = 'b'
alert(str + str.prop) // ?

结果呢?

4,隐式的全局变量

var a = 1
function func() {
    a = b = 2
}
func()
alert(a)
alert(b) // ?

JS中不用var声明的变量默认是全局变量,而这里的连等使的情况更加隐蔽。这里的b是全局的,因此func外可以访问

5,变量声明早于代码运行(Scoping and Hoisting

var uname = 'jack'
function change() {
    alert(uname) // ?
    var uname = 'lily'
    alert(uname)
}
change()

这里容易犯迷糊的是第一个alert,如果认为函数change外已经声明赋值了,此时应该是jack,实际函数内又var了一次(虽然var在其后),预解析时仍然会将其置undefined。这也是为什么书里都建议变量声明都放在代码块的最前面。

6,函数声明早于变量声明

function change() {
    alert(typeof fn) // ?
    function fn() {
        alert('hello')
    }
    var fn
}
change()

change内先alert出fn,后函数声明,再变量声明。如果fn没有函数声明而仅是变量声明,那么结果与5一样是undefined。但这里却是function。即同一个作用域内,函数声明放在代码块后面和前面都没有关系,函数可以正常使用。而变量声明则需先置前,先使用则是undefined。