十五的學習日記20160925

十五的學習日記20160925

CSS

學過盒子佈局的人都知道,元素之間的上下margin會合並,保留數值較大的margin做爲渲染依據.
可是今天在羣裏討論發現:
img元素和p元素的上下margin不會合並.
這可能代表可替換元素和其餘元素的margin存在本質區別.javascript

PHP

今天羣裏有人問我一個phpstorm提示的警告,代碼以下php

$a=0;
for($a;$a<20;$a++){
...}

IDE對for循環中第一個變量$a的警告信息爲:Expression result unused(表達式結果未使用)
然而實際運行是能夠經過的.
通過一番思考和嘗試,結論是: for循環中的第一個$a沒有賦值操做,也沒有別的操做,因此會顯示錶達式未被使用.因此改爲如下形式,就不會警告啦:css

$a=0;
for(;$a<20;$a++){//去掉了初始化聲明,就再也不警告.
...}

JavaScript

1判斷奇偶新招

過去我判斷奇偶基本靠除以2求餘數來判斷.
今天羣裏有人提了一個方法來判斷奇偶:
按位與操做 代碼&1==0,返回 true就是偶數,返回false就是偶數.java

2數組排序&去從新思路.

昨晚翻了一遍全部的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");

3讀書筆記

可調用對象不必定是函數對象
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.
未定義變量是一個已經在特定做用域中被聲明的,而還沒有被賦值的變量.app

from <You Don't know JS: type&grammar>phpstorm

4今日要點:

  1. String也有concat方法,操做規則和Array同樣.
  2. Array.from能夠直接轉化一個僞數組爲數組.
  3. 能夠經過 Array.prototype.method.call(僞數組,etc)的方式來對僞數組使用數組的方法
    String也是一種僞數組,不過由於String自己具備值的不可變性, 因此只能使用生成新數組的方法,而不能用改變String自己的方法.
  4. 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"
  1. 因爲編程語言廣泛存在0.1+0.2!=0.3的狀況.
    因此在ES6提供了浮點數比較辦法:主要用了柯西式的極限定義來比較.
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
  1. Number.MAX_VALUENumber.MAX_SAFE_INTEGER 是兩回事.
  2. voidsomething 能夠阻止賦值,替換爲undefined輸出.(ES6)
  3. NaN是數,但不存在相等性.也不能經過window.isNaN來辨別,由於window.isNaN("b")也返回true.
    不過ES6給出了Number.isNaN復了這個bug.
    另外一條思路是改寫window.isNaN爲:
isNaN = function(n) {return n !== n;};
  1. 除非該屬性特性設置爲不可修改,一切方法都不可徹底相信,若是遇到沒法排查的bug,必定要注意這點.
  2. 幾條有趣的計算
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

程序員英語

  1. You obviously must first convert (coerce) the value from number to string.
    顯然地你必須首先將這個值從數值型(強制地)轉換成字符型編程語言

  2. to be considered a flaw in the design of the language, to be shunned and avoided.
    這被認爲是一個語言設計時形成的失誤,應當被避免和制止.

  3. The value 42 has an intrinsic type of number, and its type cannot be changed
    數值42天生擁有number類型本質,而且沒法被改變.

  4. It’s tempting for most developers to think of the word 「undefined」 as a synonym for 「undeclared.」
    這很容易誘導開發者把單詞"未定義"看做是"未聲明"的同義詞.
  5. JavaScript has some unique characteristics with these types that may either delight or confound you.
    在這些類型上,js有着一些獨特的性質,可能會啓發你或迷惑你.
  6. 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=明白; 不寫了...太麻煩,今天先到這裏.

相關文章
相關標籤/搜索