學過盒子佈局的人都知道,元素之間的上下margin會合並,保留數值較大的margin做爲渲染依據.
可是今天在羣裏討論發現:
img元素和p元素的上下margin不會合並.
這可能代表可替換元素和其餘元素的margin存在本質區別.javascript
今天羣裏有人問我一個phpstorm提示的警告,代碼以下php
$a=0; for($a;$a<20;$a++){ ...}
IDE對for循環中第一個變量$a
的警告信息爲:Expression result unused
(表達式結果未使用)
然而實際運行是能夠經過的.
通過一番思考和嘗試,結論是: for循環中的第一個$a
沒有賦值操做,也沒有別的操做,因此會顯示錶達式未被使用.因此改爲如下形式,就不會警告啦:css
$a=0; for(;$a<20;$a++){//去掉了初始化聲明,就再也不警告. ...}
過去我判斷奇偶基本靠除以2求餘數來判斷.
今天羣裏有人提了一個方法來判斷奇偶:
按位與操做 代碼&1==0
,返回 true就是偶數,返回false就是偶數.java
昨晚翻了一遍全部的Array.prototype
方法,想了一個新招來處理數組排序&去重,代碼以下:程序員
function reducedup(array){ if(array.constructor.name!="Array"){ array=array.toString().split(""); } return array .sort(function (a,b) {return a-b;}) .filter(function (el,i,arr) { if((i<arr.length-1)&&el!==arr[i+1]|| (i==arr.length-1)&&el!==arr[i-1]){ return el;}}); } reducedup("8543217486765379534279089865314");
可調用對象不必定是函數對象
a function is referred to as a 「callable object」—an object that has an internal [[Call]] property that allows it to be invoked.
函數是一個可調用對象,也就是說,是一個具備內建特性[call]
的對象,以致於讓它能夠被調用.編程It’s most appropriate to think of them also as a 「subtype」 of object (see Chapter 3), in this case with the additional characteristics of being numerically indexed (as opposed to just being string-keyed like plain objects) and maintaining an automatically updated .length property.
(數組)能夠適當地把它想象成是Object類型的子類, 並加上了一些額外的性質,好比擁有可數下標,好比自動增加的.length屬性.數組An 「undefined」 variable is one that has been declared in the accessible scope, but at the moment has no other value in it.
未定義變量是一個已經在特定做用域中被聲明的,而還沒有被賦值的變量.appfrom <You Don't know JS: type&grammar>phpstorm
String
也有concat
方法,操做規則和Array
同樣.Array.from
能夠直接轉化一個僞數組爲數組.Array.prototype.method.call(僞數組,etc)
的方式來對僞數組使用數組的方法String
也是一種僞數組,不過由於String
自己具備值的不可變性, 因此只能使用生成新數組的方法,而不能用改變String
自己的方法.Number.prototype.toFixed(int)
返回固定小數點int位置的值,而且轉換爲String
.Number.prototype.toPrecision(int)
也有一樣效果,只不過他計算的是整個數值的長度.var a = 42.59; a.toFixed( 0 ); // "43" a.toFixed( 1 ); // "42.6" a.toFixed( 2 ); // "42.59" a.toFixed( 3 ); // "42.590" var a = 42.59; a.toPrecision( 1 ); // "4e+1" a.toPrecision( 2 ); // "43" a.toPrecision( 3 ); // "42.6" a.toPrecision( 4 ); // "42.59"
0.1+0.2!=0.3
的狀況.function numbersCloseEnoughToEqual(n1,n2) { return Math.abs( n1 - n2 ) < Number.EPSILON;} var a = 0.1 + 0.2,b = 0.3; numbersCloseEnoughToEqual( a, b ); // true numbersCloseEnoughToEqual( 0.0000001, 0.0000002 ); // false
Number.MAX_VALUE
和Number.MAX_SAFE_INTEGER
是兩回事.void
something
能夠阻止賦值,替換爲undefined
輸出.(ES6)NaN
是數,但不存在相等性.也不能經過window.isNaN
來辨別,由於window.isNaN("b")
也返回true.Number.isNaN
復了這個bug.window.isNaN
爲:isNaN = function(n) {return n !== n;};
Infinity *or+ Infinity //Infinity Infinity /or- Infinity //NaN Infinity/0 //Infinity Infinity*0 //NaN 1/Infinity //0 0/-3 //-0 JSON.stringify( -0 ); // "0" JSON.parse( "-0" ); // -0 -0===0 //true
You obviously must first convert (coerce)
the value from number to string.
顯然地你必須首先將這個值從數值型(強制地)轉換成字符型編程語言
to be considered a flaw in the design of the language, to be shunned
and avoided.
這被認爲是一個語言設計時形成的失誤,應當被避免和制止.
The value 42 has an intrinsic
type of number, and its type cannot be changed
數值42天生擁有number類型本質,而且沒法被改變.
tempting for
most developers to think of the word 「undefined」 as a synonym
for 「undeclared.」confound
you.However, a gotcha
to be aware of is that if a string value intended as a key can be coerced to a standard base-10 number
要明白的一點是,若是你想用一個字符串型的值做爲鍵值,那它會被強制轉換成10進制數.
Coerce
:強制,強迫;
Shunned
:避免,躲開;
intrinsic
固有的本質的;
tempting for
引誘,誘導;
synonym
同義詞;
confound
:迷惑,困惑.
gotcha
:=got you=明白; 不寫了...太麻煩,今天先到這裏.