十五的学习日记20160925

十五的学习日记20160925

CSS

学过盒子布局的人都知道,元素之间的上下margin会合并,保留数值较大的margin作为渲染依据.
但是今天在群里讨论发现:
img元素和p元素的上下margin不会合并.
这可能表明可替换元素和其他元素的margin存在本质区别.

PHP

今天群里有人问我一个phpstorm提示的警告,代码如下

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

IDE对for循环中第一个变量$a的警告信息为:Expression result unused(表达式结果未使用)
然而实际运行是可以通过的.
经过一番思考和尝试,结论是: for循环中的第一个$a没有赋值操作,也没有别的操作,所以会显示表达式未被使用.所以改成以下形式,就不会警告啦:

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

JavaScript

1判断奇偶新招

过去我判断奇偶基本靠除以2求余数来判断.
今天群里有人提了一个方法来判断奇偶:
按位与操作 代码&1==0 ,返回 true就是偶数,返回false就是偶数.

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.
未定义变量是一个已经在特定作用域中被声明的,而尚未被赋值的变量.

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

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. void something 可以阻止赋值,替换为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=明白;
不写了...太麻烦,今天先到这里.

原文地址:https://www.cnblogs.com/always-naive/p/5933826.html