js Flashcards

1
Q

如果想让事件由上至下

A

xx.addEventListener(‘click’, fn, true);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

如果上下都有事件,想要执行上面事件后阻止下面事件?

A

event.stopPropagation();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

open a link in js?

A

same window:window.location = url;new window:window.open(url);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to break out of a forEach loop?

A

you can’t.but you could break out of a some/every by returning false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

zepto在requirejs引用的时候……

A

不能带$配对:define([‘zepto’], function() { console.log($)});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to detect browser:

A

create a test element to see if there exists such a property, so we could detect if the browser is -webkit or -moz, and only use this one prefix instead of writing four to cover all:var testEl = document.createElement(‘div’);$.each(vendors, function(vendor, event) { if (testEl.style[vendor + ‘TransitionProperty’] !== undefined) { prefix = ‘-‘ + downcase(vendor) + ‘-‘; eventPrefix = event; return false; }});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to use jQuery’s animation?

A
  1. 四个参数:properties(object), duration(1000), ease(string), callback2. 两个参数:properties(object), 其他设置参数(object), 比如duration,epecialEasing,complete……
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to test if an argument is plain object or not?

A

var a = [], d = document, o = {};typeof a; //objecttypeof b; //objecttypeof c; //objectjQuery.isPlainObject( a ); //falsejQuery.isPlainObject( b ); //falsejQuery.isPlainObject( c ); //true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

tweenMax’s fromto?

A

TweenLite.fromTo(xx, 1.5, {width:0, height:0}, {width:100, height:200});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to stop setInterval?

A

with clearInterval:var nameOfInterval = setInterval(function(){ if (xxx) { clearInterval(nameOfInterval); }, 100);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what to pay attention on tablet and mobile?

A
  • notouch(roll over), -webkit-tap-highlight-color: rgba(0,0,0,0); * mobile横屏文字变大禁止:-webkit-text-size-adjust: none; * 视频不要太大,宽高,不然播放不了
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

String.replace?

A

“someString”.replace(/(xx)(yy)/, function(match, p1, p2, offset, string) { return [p1, p2].join(“ “);}match: 符合搜索条件的字段p1: 第一个括号里的符合字段p2: 第二个括号里的符合字段offset: p1的index numberstring: 整个string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

用事件传送数据:

A

//set an anchor for future event binds (in order to transmit cross page data)window.anchor = {};$(anchor).on(“transmit”, function(){ return {};});$(anchor).trigger(“transmit”);或者这个锚点就可以直接用$(‘html’),避免冲突,而且哪里都能取到。

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

requirejs grunt build, what would be compressed, and what would not?

A

只有写在main的define里,才会被压缩进来,在main里require的,文件会单独载入。

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

tweenMax有时候会闪一下

A

可能是因为某个动画设置了delay

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what to be careful about with for..in loop?

A
  1. 用在object上:有可能会把继承来的property也iterate出来,所以若想只用这个object自己的property,就用hasOwnProperty。2. 它的iterate顺序是随机的,所以,用在array上可能会有很神奇的效果。array用forEach(e, i, a)for..in should not be used to iterate over an Array where index order is important. Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that for…in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited. Because the order of iteration is implementation dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index (or Array.forEach or the non-standard for…of loop) when iterating over arrays where the order of access is important.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

careful, tweenMax and IE8?

A
  1. 控制上层div opacity变成0,下层会变化。上层div opacity变成1,下层不会变。2. 如果设置的css是%为单位,在TweenMax中设置的又是动画到某个数值(没有设置单位),那ie8会把它理解为同样的单位,%。解决:在TweenMax中指明,是某个px,而非直接写数值3. 有的时候,css已经设置了opacity:0,ie8就是不显示。这时候可以在js里加一个设置 opacity:.01,手动让它出现。
18
Q

zoom/scale in IE8? (tweenmax)

A

use position: absolute, 因为它会用left,top来算.

19
Q

ie8 and video tag:

A

IE8 不支持新的标签,比如video标签,所以videojs取dom src可能会出问题。解决: * 自己声明“我新建了标签喔” * 必须是在header里声明用html5.js/*! HTML5 Shiv vpre3.6 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed Uncompressed source: https://github.com/aFarkas/html5shiv */

20
Q

how to use indexOf, and problem with it?

A
  1. [a, b, c].indexOf(a) ===> 02. ie8 doesn’t support that. needs to add it in js(已经加入config.js)if (!Array.prototype.indexOf) { …}
21
Q

jQuery.each()

A

jQuery.each(arr, function(e, i) { //”this” would be arr}will jump out of the loop if returned “false”

22
Q

jQuery: $dom.each()

A

$(‘li’).each(function( i ) {})如果是对dom object的直接each,就用jquery内置的each比较方便。 * 不过记得,如果里面有用到settimeout的话,里面的$(this)还需要用$that来替换,不然会指向$(window)

23
Q

什么叫图片重绘?

A

chrome为了节省内存,在解码图片(将png、jpg按照某种规则绘制成像素点)后,如果过一段时间不需要用它,就会将其回收,下次出现还会再次解码。解决方案:用canvas,强制其不回收

24
Q

setTimeOut是很神奇的生物

A

在很多个东西都需要settimeout的时候,用,即使t传入的是0,也会延迟。

25
Q

如果动画感觉很奇怪,easing怎么调也不对……

A
  • css easing, transition * js animation相抵触。掀桌!!!!!!!
26
Q

循环引用?

A

一个page下面有很多tab,两个都是通过new的constructor创建的。里面tab.page也可以链接到page的this,通过传参传入。等于是,父亲和孩子能相互链接到彼此。

27
Q

call/apply?

A

myFunction.call(scope)function(a){ return this.a;}.call(window, x);equals to:(function(x){ return window.x;})(x);Analogy: * 就好像指定小孩玩耍的后院 * 就好像让游乐园里的飞翔座椅绕着中心杆飞 * 把鱼倒进池塘里 * You use call or apply when you want to pass a different this value to the function. In essence, this means that you want to execute a function as if it were a method of a particular object. * it’s like the difference between $.each and xxx.each(fn)

28
Q

jQuery: hover, unhover

A

xx.hover(function(){}, function(){});前一个是hover的效果,后一个是unhover的效果。等同于:xx.mouseenter(function(){})xx.mouseleave(function(){})要写就写一组,一个写hover一个写mouseleave会有奇怪的问题。

29
Q

=/==/===

A

i used = instead of === again!!! in if.wtf… Lesson learned. :) * and yet second time in a day. grrrrr.

30
Q

js效能,提高效率

A
  • 写var的时候要写在一起因为每用jquery取一个数值(宽、高等),它都会把div所有的数值都检查一遍。只要没有数值改变,再取就不会耗时。但是一旦某一个数值改变了,再取数值就会所有重新都取一遍。 * update刷帧的function要特别小心d因为每一个都执行n遍啊。 * 取jquery对象尤其expensiveinstead of using “$dom.toggleClass”, better use “$dom[0].classList.toggle” * 滚动时禁止其他操作(hover)
31
Q

trigger event in js/jquery?

A
  • js$(function(){ window.dispatchEvent(new Event(‘resize’));})* jquery$(window).trigger(‘resize’);
32
Q

在chrome中,有的时候鼠标移动过一个区域的时候,它会抖一下,或者不相干的div也会闪烁。

A

解决: * 给闪烁的div,或者它的父集,加上-webkit-backface-visibility: hidden;(android2.3不能加)解释: * 就是webkit的渲染问题。

33
Q

why using setTimeOut in for loop is a bad idea

A

因为虽然每次传入的参数i不一样,但是到最后setTimeout里面的function执行的时候,i已经全部都是最后一个数了。

34
Q

toString?

A
  1. Boolean: “true”/”false” 2. Number: “19”/”101011”/”AF10D”,可以转换进制 3. Function: 把代码本身打印出来,method也是 4. Object: 返回 “[object object]”,嗯,不是很有用了
35
Q

can’t use this in for loop?

A

proto.init = function() { proto.insertDom();};use this inside of the function, not this奇怪的错误

36
Q

why put mediaelement in head?

A

because it needs to declare some html5 tags for ie8, in which case, just set the html5js in header would be fine

37
Q

why firefox doesn’t use mediaelement to load the video, saying, HTTP “Content-Type” of “video/mp4” is not supported. Load of media resource xxx failed?

A

it was due to the src attr written in the video tag.solve: remove it when platform is firefox, or remove it altogether

38
Q

what to pay attention to recursion in js?

A

in each layer of functions, you need to have a “return”.and if the condition looks something like ajax: $.ajax({ url: ‘images/xx.jpg’, success: function() {}, error: function() {}});with success and error, this might hinder the condition. so do not use return. instead, use this.x = xx instead to store variables.

39
Q

how to check if a file exists?

A

$.ajax({ url: ‘images/xx.jpg’, success: function() {}, error: function() {}});

40
Q

convert strings to numers?

A

parseInt( string [, base] )

41
Q

how to smoothly scroll to some position on clicking some button?

A
  1. load TweenMax.ScrollToPlugin2. TweenMax.fromTo($window, .75, { scrollTo: {y: $window.scrollTop(), autoKill:true},}, { scrollTo: {y: scroll, autoKill:true}, ease: Power3.easeOut, overwrite: 5 });autoKill: true意思是,如果scroll到一半,用户有scroll动作了,就杀死原来的scroll。