Skip to content

Instantly share code, notes, and snippets.

@wcc526
Created September 26, 2015 12:10
Show Gist options
  • Save wcc526/4f6c0586d3bdc3fcb016 to your computer and use it in GitHub Desktop.
Save wcc526/4f6c0586d3bdc3fcb016 to your computer and use it in GitHub Desktop.
javascript.md

switch

var grade = "Preminum";

switch(grade){
    case "Regular":
        alert("It is ok");
    case "Priminum":
        alert("It is ok");
    case "Diesel":
        alert("It is ok");
    default:
        alert("Default");
}

indexOf

查找

var phrase = "We want a groovy keyword.";
var position = phrase.indexOf("groovy"); // 10
// if position == -1 is not found

slice

分割

substring substr

Regex

var myRE = /^hello/; // ^ at the start
           /hel+o/;  // + once or more
           /hel*o/;  // * zero or more
           /hello|goodbye/; // either|or
           /he..o/; // . any character
           /\wello/' // \w alphanumeric or _
           /[crnld]ope/; // [...] range of chars
           /^[0-9]{5}(?:-[0-9]{4})?$/
           /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/

Array

var myArray = [10,"a",1,40,50];
myArray.reverse();
       .sort();
       .join();
var lastElement = myArray.pop();
myArray.push(123);

var i=0;
while(i < myArray.length){
    alert("The value is:" + myArray[i]);
    i++;
}

DOM

var headline = document.getElementById("mainHeading");
headline.innerHTML = "Wow,a new headline!";
var headline = document.getElementById("mainHeading");
headline.onclick = function(){
    headline.innerHTML = "You clicked it";
}

Self-Executing Anonymous functions

SEAF

Self-Invoking Anoymous Functions Immediately Invoked Function Expressions

(function($){

    "use strict";

    // not leaked to the global scope
    var _cached ={};
})(jQuery);

parseInt toString

var foo = parseInt("1234",10);
var foo = parseFloat("3.14",10);
var num = 8;
var str = num.toString();

object

var person = new Object();

person.firstName = "Jeremy";
person.lastName = "McPeak";
person.getFullName = function(){
    return this.firstName + " " + this.lastName;
}
alert(person.getFullName());
var person = {
    firstName : "Jeremy",
    lastName : "McPeak",
    getFullName : function(){
        return this.firstName + " " + this.lastName;
    }
}

Search

http://regexlib.com/Search.aspx

var myString = "my zip is 017210-1234 what is yours?";
var offSet = myString.search(/\b[0-9]{5}(?:-[0-9]{4}?\b/);
alert(offset);

typeof

var value = null;
alert(typeof value); //"object"

使用 === 而不使用 ==

== 如果类型不相等会进行强制类型转换
=== 类型不相等直接返回False

Replace

var s = "javascript";
var b = s.replace( /(java)(script)/, "$2-$1");
alert(b);
var r = /(<.*?>/g;
var a = s.match(r);
for(var i = 0; i < a.length ; i ++){
    alert(a[i]);
}

HTML 集合

document.getElementsByName()
document.getElementsByClassName()
document.getElementsByTagNameNS()
document.images
document.links
document.forms
document.forms[0].elements

# 使用css 选择器
document.querySelectorAll('#menu a');
document.querySelectorAll('div.warning,div.notice');

var cool = document.getElementsByTagName_r('div');
# 注意缓存数组,集合长度每次遍历都会计算一遍
len = cool.length;
# 注意缓存局部变量
el = coll[count];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment