This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function f(){ | |
return this.a; | |
} | |
var a = {a: 'foo'}; | |
var b = {a: 'bar'}; | |
var x = f.bind(a); | |
var y = x.bind(b); | |
// bind 只能绑定一次, 可重复绑定 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# editorconfig.org | |
root = true | |
[*] | |
indent_style = space | |
indent_size = 2 | |
end_of_line = lf | |
charset = utf-8 | |
trim_trailing_whitespace = true | |
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// stop capture | |
ul.addEventListener('click', function(e) { | |
if(e.currentTarget === e.target) { | |
console.log(e.target); | |
} | |
}); | |
// stop bubbling | |
li.addEventListener('click', function(e) { | |
console.log(e.currentTarget); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function styleHyphenFormat(propertyName) { | |
function upperToHyphenLower(match) { | |
return '-' + match.toLowerCase(); | |
} | |
return propertyName.replace(/[A-Z]/g, upperToHyphenLower); | |
} | |
function cssStyle2DomStyle(sName) { | |
let result = sName.replace(/-\w/g, function (match) { | |
return match.slice(1).toUpperCase(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert a number to a hexadecimal string with: | |
hexString = yourNumber.toString(16); | |
// and reverse the process with: | |
yourNumber = parseInt(hexString, 16); | |
// Example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = 'Hello World 你好'; | |
str.match(/[\u0256-\uffff]/g) | |
// (2) ["你", "好"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function commonParentNode(oNode1, oNode2) { | |
if (oNode1.contains(oNode2)) { | |
return oNode1; | |
} else { | |
return commonParentNode(oNode1.parentNode, oNode2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {String} HTML representing a single element | |
* @return {Element} | |
*/ | |
function htmlToElement(html) { | |
var template = document.createElement('template'); | |
html = html.trim(); // Never return a text node of whitespace as the result | |
template.innerHTML = html; | |
return template.content.firstChild; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let body = document.querySelector('body'); | |
let bodyStyle = window.getComputedStyle(body); | |
Object.prototype.toString.call(bodyStyle); | |
// "[object CSSStyleDeclaration]" | |
console.log('body background: ' + bodyStyle.getPropertyValue('background'); | |
body.style.background = '#000'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = new Function('a', 'b', 'return a + b'); | |
x(1,2); | |
// 3 |