美文网首页
细节 javascript 使用的技巧

细节 javascript 使用的技巧

作者: alipy_258 | 来源:发表于2020-03-08 14:52 被阅读0次

今天看一篇文章,学到了很多,好记性不如烂笔头~

字符串转换为数字
var str = '111';
console.log(+str); // 111
console.log(str-0); // 111
console.log(typeof +str); // number
console.log(typeof (str-0)); // number
// 日期也可以
var data = +new Date(); // 1555584126215
数值向下取整
var a = ~~2.50; // 2
var a= 2.50>>0; // 2
var a= 2.50|0; // 2
Math.floor(2.50); // 2
字符串取整
var a = "2.50"|0; // 2
var a= "2.50"^0; // 2
parseInt(2.50); // 2
使用 && 替代单一的条件判断
// 可以这样
if(!ticket) {
    getLogin();
}
// 也可以这样写
!ticket && getLogin();
// 或
ticket || getLogin();
通过闭包调用setTimeout
for(var i = 0; i < 10; i++) {
    setTimeout(function(){
        console.log(i);  // 10 10 10 ...
    },1000);
}

for(var i = 0; i < 10; i++) {
    (function(i){
        setTimeout(function(){
            console.log(i);  // 0 1 2 3 4 ...
        },1000)
    })(i);
}
时间日期格式转换函数
// 实现方法:在 Date 原型上添加方法
Date.prototype.format = function(format) { 
    var o = {
        'M+': this.getMonth() + 1,                      //month
        'd+': this.getDate(),                           //day
        'h+': this.getHours(),                          //hour
        'm+': this.getMinutes(),                        //minute
        's+': this.getSeconds(),                        //second
        'q+': Math.floor((this.getMonth() + 3) / 3),    //quarter
        'S': this.getMilliseconds()                     //millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(
            RegExp.$1,
            (this.getFullYear() + '').substr(4 - RegExp.$1.length)
        );
    }
    for (var k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(
                RegExp.$1,
                RegExp.$1.length == 1 ?
                    o[k]: ('00' + o[k]).substr(('' + o[k]).length)
            );
        }
    }
    return format;
}

// 使用方法
var now = new Date(); 
var nowStr = now.format('yyyy-MM-dd hh:mm:ss'); 

var testDate = new Date(); 
var testStr = testDate.format('YYYY年MM月dd日hh小时mm分ss秒'); 
alert(testStr); 

alert(new Date().format('yyyy年MM月dd日')); 
alert(new Date().format('MM/dd/yyyy')); 
alert(new Date().format('yyyyMMdd')); 
alert(new Date().format('yyyy-MM-dd hh:mm:ss'));
格式化JSON

你之前可能使用过 JSON.stringify,但你是否知道它还可以用来给 JSON 添加缩进?
stringify() 方法可以接受两个额外的参数,一个是函数(形参为 replacer),用于过滤要显示的 JSON,另一个是空格个数(形参为 space)。
space 可以是一个整数,表示空格的个数,也可以是一个字符串(比如’ ’表示制表符),这样得到的 JSON 更容易阅读

console.log(JSON.stringify({ name: '惠丽', id: '123' }, null, '   '));
// Result:
// '{
//  "name": '惠丽',
//  "id":'123'
// }'

参考链接:https://blog.beard.ink/tags/JavaScript/

相关文章

  • 细节 javascript 使用的技巧

    今天看一篇文章,学到了很多,好记性不如烂笔头~ 字符串转换为数字 数值向下取整 字符串取整 使用 && 替代单一的...

  • [稀土掘金日报] JavaScript 开发者必备的资源合集

    JavaScript 开发者必备的资源有哪些?还有 JavaScript 日常使用小技巧!以及如何成为一个Java...

  • 10 个很实用的 JavaScript 技巧

    编程语言通常暗藏着各种技巧,熟练使用这些技巧可以提高开发效率。JavaScript 就是一门技巧性很强的语言,掌握...

  • windows使用技巧

    window使用技巧 muggle [六个核弹](javascript:void(0);) 2018-11-19 ...

  • JavaScript小技巧:!!的使用

    今天看Vue.js源代码,其中有一段和enumerable相关的代码,如下所示: 其中!!enumerable中"...

  • Javascript的使用小技巧

    利用解构来进行数据交换 判断数据的类型 获取纯数字数组的最大最小值,主要使用apply来传递参数 数组去重 将伪数...

  • JavaScript Promise 的使用技巧

    “生产代码”是可能需要一些时间来执行的代码。 “消费代码”是必须等待结果的代码。 Promise 是一个 Java...

  • 现代JavaScript使用技巧

    函数参数设置默认值 为函数设置默认值很简单,直接在参数名后面加等号及值,如果该参数没有传参的话函数就会去取默认值,...

  • JavaScript 使用小技巧

    1.删除数组中的假值 我们时常需要在处理数据的时候要去掉假值。在Javascript中,假值是false, 0, ...

  • 移动web端学习(二)

    一、基础知识 1.调试技巧: 2.web开发JavaScript使用技巧1)屏蔽鼠标右键 2)取消选取,防止复制 ...

网友评论

      本文标题:细节 javascript 使用的技巧

      本文链接:https://www.haomeiwen.com/subject/kjtxgqtx.html