javascript tips and snippets

  • 数字化的Date

js中,Date是内置的一种数据类型,虽然直接输出的date格式适合人们阅读,但是对于机器程序来说却有些不方便:

var now=new Date();
console.log(now);//Wed Nov 25 2015 22:02:17 GMT+0800 (中国标准时间)

怎么转换为便于js程序来操作的形式呢?很简单只要在new关键字前面加上一个+即可:

var now=+new Date();
console.log(now);//1448460291345

javascript Errors:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

try {
  foo.bar();
} catch (e) {
  if (e instanceof EvalError) {
    alert(e.name + ‘: ‘ + e.message);
  } else if (e instanceof RangeError) {
    alert(e.name + ‘: ‘ + e.message);
  } else if (e instanceof InternalError) {
    alert(e.name + ‘: ‘ + e.message);
  }else if (e instanceof ReferenceError) {
    alert(e.name + ‘: ‘ + e.message);
  }else if (e instanceof SyntaxError) {
    alert(e.name + ‘: ‘ + e.message);
  }else if (e instanceof TypeError) {
    alert(e.name + ‘: ‘ + e.message);
  }else if (e instanceof URIError) {
    alert(e.name + ‘: ‘ + e.message);
  }
  // ... etc
}
时间: 2024-08-03 07:49:16

javascript tips and snippets的相关文章

Javascript Tips & Tricks

前端开发规范系列文章之Javascript Tips and Tricks,本意是写成常用代码收集.常用技巧整理的文章,感觉"常用代码大全"太土."实用代码整理"有失偏颇,"提示与技巧"不够稳重,所以使用常用的英语说法,相信广大程序员都懂得. 妙味 Javascript美妙之处,需要我们静静体会,慢慢吸收,然后让代码在您指下曼舞.整理的这些代码我们称之为妙味,请大家细品. 博主会不断更新本文,方便大家阅读起见,我们采用倒序更新的方式,把最新更新的

45 Useful JavaScript Tips, Tricks and Best Practices(有用的JavaScript技巧,技巧和最佳实践)

As you know, JavaScript is the number one programming language in the world, the language of the web, of mobile hybrid apps (like PhoneGap or Appcelerator), of the server side (like NodeJS or Wakanda) and has many other implementations. It’s also the

JavaScript Tips

# JavaScript Tips -------------------------- 1. return false - event.preventDefault();  //阻止默认行为 P.S 阻止a标签的跳转 - event.stopPropagation(); //阻止事件冒泡 2. reset 事实上很少需要用到reset,多数需求为返回上一页面 P.S 注册时不打算注册了,而不是想要把之前填的很多内容清空 3. form1.elements[0] or name 使用方便,当然当

JavaScript tips:window.onload与$(document).ready()的差异

1.window.onload在$(document).ready()之后执行 原因:二者触发与否,都是依据document.readyState这个属性. (1)document.readyState的值:uninitialized.loading.interactive.complete(这四个值,依据时间先后排序). (2)当其值为interactive时,会触发documentContentLoaded事件,JQuery.ready()就是对documentContentLoaded事件

JavaScript Tips: 原型链 prototype

当Bar.prototype = new Foo(); $(function () { var test = new Bar(); console.log(test);//Bar test.method();//method log console.log(test.foo);//Hello World console.log(test.value);//42 var footest = new Foo(); console.log(footest);//Foo console.log(foot

JavaScript tips:数组去重

1.实现目标:数组去重 2.实现思路: (1)创建新数组. (2)遍历原数组,判断当前被遍历元素是否存在于新数组,如果存在于新数组,则判断当前被遍历元素是重复的:如果不存在于新数组,则判断当前被遍历元素不是重复的,则将其压入新数组. (3)遍历完毕原数组后,返回新数组. 3.具体实现:根据对当前元素是否重复的判断方法不同,有四种不同的具体实现. (1)利用hash表保存被遍历元素是否重复的状态. 1 function unique( arr ) { 2 var n = {}, //hash表,用

25+ Useful Selenium Web driver Code Snippets For GUI Testing Automation

本文总结了使用Selenium Web driver 做页面自动化测试的一些 tips, tricks, snippets. 1. Chrome Driver 如何安装 extensions 两种方式 a) Packed (.crx file) --  crx为Chrome的插件后缀名,FireFox的是xpi ChromeOptions options = new ChromeOptions(); options.addExtensions(new File("/path/to/extensi

45个实用的JavaScript技巧、窍门和最佳实践

如你所知,JavaScript是世界上第一的编程语言,它是Web的语言,是移动混合应用(mobile hybrid apps)的语言(比如PhoneGap或者Appcelerator),是服务器端的语言(比如NodeJS或者Wakanda),并且拥有很多其他的实现.同时它也是很多新手的启蒙语言,因为它不但可以在浏览器上显示一个简单的alert信息,而且还可以用来控制一个机器人(使用nodebot,或者nodruino).掌握JavaScript并且能够写出组织规范并性能高效的代码的开发人员,已经

45种Javascript技巧大全

原文:45 Useful JavaScript Tips, Tricks and Best Practices 译文:45个有用的JavaScript技巧,窍门和最佳实践 译者:dwqs 在这篇文章中,我将分享一些JavaScript常用的技巧,窍门和最佳实践.不管JavaScript开发者是使用在浏览器/引擎上或者服务器端(SSJS--Service Side JavaScript)JavaScript解释器上,这些他们都是应该知晓的. 需要注意的是,文章中的代码片段均是在最新的Google