十五的学习日记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=明白; 不写了...太麻烦,今天先到这里.

相关文章
相关标签/搜索