Test Record
来源:互联网 发布:淘宝直播好申请吗 编辑:程序博客网 时间:2024/06/06 23:16
1. Type
10 + 20/0//Infinity'10' + 20/''//10Infinity
2. Variate
(1) not defined
var foo = "hello";(function(){ var bar = " world"; alert(foo + bar);})();alert(foo + bar);//-hello world //-error: bar is not defined
如果var 了bar,但没有赋值,才是 undefined。
没有var 就是 not defined。
(2) Are the following statements equivalent, if not, what’s the difference
var a = b = c = 1;var c = 1, b = c, a = b;//No, the former one creates global variables b and c.
3. SetTimeout
(function () { console.log(1); setTimeout(function () { console.log(2); }, 100); console.log(3); setTimeout(function () { console.log(4); }, 0); console.log(5);})()//1, 3, 5, 4, 2
4. Join
(function () { var a = [1]; function bar() { if (!a) { var a = [1, 2]; } console.log(a.join()); } bar();})()//1, 2
没有块级作用域,只有函数作用域。作用域中变量声明会前置。
5. Write the ‘add’ function that makes the following statement true:
add(3,4) === add(3)(4)
function add() { var arg1 = arguments [0]; return arguments.length == 2 ? arg1 + arguments[1] : function (arg2) { return arg1 + arg2; }}
如何理解这样的代码?我们做一个翻译
function add(){ var value; var arg1 = arguments[0]; if(arguments.length==2){ value = arg1+arguments[1]; }else{ value = function(arg2){ return arg1+arg2; } } return value;}
if的部分很好理解,主要在于else部分。疑惑主要存在于,arg2并没有赋值,如何获得第二个参数的值。
add(3)//运行结果为function(arg2){ return arg1+arg2;}
//add(3)(4)等价于(仅帮助理解,但有区别)//如下取不到arg1的值,因为arg1的作用域在add()中)(function(arg2){ return arg1+arg2;})(4)
6. Write the ‘increase’ function that makes the following statements true (hint: please use JavaScript closure):
increase() === 0;increase() === 1;increase() === 2;…
var increase=(function(){ var count=0; return function(){ return count++; }})();
运用闭包,count变量不会被释放,在定义后会一直在内存中。到底定义的increase是什么样的。运行increase,可以看到,它实际是一个函数。
//increasefunction(){ return count++;}
7. Write a plugin that dynamically sets the background-image of a DIV element
called c-hero. This plugin should work generically for any website project. You may use CoffeeScript or regular JS syntax for your code.
You should consider best practices in your solution.
使用jQuery的plugin来写,另外,图片做示例不方便,下代码改为用颜色代替image。
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body> <div class="c-hero" style="height: 200px;width: 200px;"> </div></body><script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js"></script><script> (function($){ $.fn.setBackground= function(options){ var settings = $.extend({ mobile: "#000", desktop: "#f0f" },options); var img; if(window.innerWidth < 768){ img = settings.mobile; }else{ img = settings.desktop; } return this.css({ backgroundColor: img }); } })(jQuery); $('.c-hero').setBackground();</script></html>
0 0
- Test Record
- 08 scala, imported `Record' is permanently hidden by definition of class Record in package test
- Troubleshooting Record and Playback issues in Coded UI Test
- 【Study Record】Using Google Mocking Framework in Unit Test ( Advanced )
- 使用Record Espresso Test功能自动生成测试代码
- record- -
- record
- record
- record
- Record
- Record
- record
- record
- Record
- RECORD
- record
- record
- record
- 微信小程序第一手实践分享(作者:黄家伟)
- 2016迈向“梦”的另一头,2017踏上“梦”的彼岸。
- Java中对象的深复制(深克隆)和浅复制(浅克隆)介绍
- 富文本编辑器 CKeditor 配置使用
- JVM运行时刻内存的分配详解
- Test Record
- 笔记本电脑静音后再取消静音,外设会没声音解决
- 【Java面试】基础知识篇
- 欢迎使用CSDN-markdown编辑器
- SQLMode及相关问题
- bashrc与profile的区别
- 使用SDImageCache优化用户体验
- Spring单元测试系列
- Vuejs入门级简单实例