获取form的所有的表单元素

document.forms['eg'].elements;
$.map($('[name=eg]').serializeArray(), function(item, index){

return $('[name='+item['name']+']');

});

replace第一个参数拼接变量

replace(new RegExp(this.value, 'g'),''+this.value+'');

函数this

setInterval(_this.autoPlay.bind(_this),3000);
setInterval("_this.autoPlay()",3000);如果第一个参数是字符串的话,类似eval

function Pig() {};
Pig.prototype={

init:function () {},
move:function () {},
render:function(){
this.move();
console.log(this)
 
}

}

var pigPlayer=new Pig();

    setInterval(pigPlayer.render.bind(pigPlayer),1000);
    setTimeout(function(){pigPlayer.render()},1000)

获取URL中的参数

var urlParams; 
(window.onpopstate = function() {
    var match,
    pl = /\+/g,
    search = /([^&=]+)=?([^&]*)/g,
    decode = function(s) {
        return decodeURIComponent(s.replace(pl, " "));
    },
    query = window.location.search.substring(1);
    urlParams = {};
    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
})();
var a =document.createElement('a');
a.href='http://www.cnblogs.com/season-huang/index?param=yes&article=1';
query = a.search.substring(1);
urlParams = {};
arr.map(function(i){
    urlParams[i.split('=')[0]]=i.split('=')[1]
});
console.log(urlParams)
//Object {param: "yes", article: "1"}
var paramsString = "q=URLUtils.searchParams&topic=api"
var searchParams = new URLSearchParams(paramsString);
searchParams.get('q')

闭包

var apples = ["apple1", "apple2", "apple3"];
for (var i = 0, funs = []; i < 3;i ++){
    funs[i] = function(){
        console.log(apples[i]);
    }
}
funs[0]();
funs[1]();
funs[2]();//undefined

var apples = ["apple1", "apple2", "apple3"];
for (var i = 0, funs = []; i < 3;i ++){
    funs[i] = (function(x){
        return function(){
        console.log(apples[x]);
        }
    })(i);
}
funs[0]();
funs[1]();
funs[2]();//apple3

钱换啤酒

function computed(money) {

var num = parseFloat(money) / 2;
var pingzi = num, gaizi = num;
var total = num;
reComputed();
function reComputed() {
    if (pingzi < 2 && gaizi < 4) {
        return;
    }
    if (pingzi >= 2) {
        var beishu = Math.floor(pingzi / 2);
        pingzi = pingzi - beishu * 2 + Math.floor(pingzi / 2);
        gaizi = gaizi + beishu;
        total = total + beishu;
    } else {
        var beishu_1 = Math.floor(gaizi / 4);
        gaizi = gaizi - beishu_1 * 4 + Math.floor(gaizi / 4);
        pingzi = pingzi + beishu_1;
        total = total + beishu_1;
    }
    reComputed();
}

return total;

}

javascript黑点

[clipboard.png

clipboard.png

clipboard.png

parseInt(0.00000008)//8
parseInt(0.000008)//0
对应python
>>> 0.2.hex()
'0x1.999999999999ap-3'
>>> (0.8 - 0.6).hex()
'0x1.999999999999cp-3'
>>> 0.1.hex()
'0x1.999999999999ap-4'
>>> (0.8 - 0.7).hex()
'0x1.99999999999a0p-4'
>>> (0.2 - 0.1).hex()
'0x1.999999999999ap-4'
> 0 >= null
true
> 0 <= null
true
> 0 == null
false
> 0 === null
false
> [] == []
false
> [] === []
false

苏生不惑
18.9k 声望1.9k 粉丝