Skip to content

Instantly share code, notes, and snippets.

View cheng470's full-sized avatar
🌴
On vacation

cheng470 cheng470

🌴
On vacation
View GitHub Profile
@cheng470
cheng470 / css-float-three-clomns.html
Last active August 29, 2015 14:07
流式三列布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
text-align: center;
}
.wrapper {
@cheng470
cheng470 / loadScript.js
Created October 9, 2014 23:38
Dynamic Script Elements 动态脚本元素
function loadScript(url, callback){
var script = document.createElement ("script") ;
script.type = "text/javascript";
if (script.readyState){ //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" || script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
@cheng470
cheng470 / odd.coffee
Created October 10, 2014 01:07
判断是否是奇数,学习函数定义
odd = (num) ->
unless typeof num is 'number'
throw "#{num} is not a number"
unless num is Math.round num
throw "#{num} is not an integer"
unless num > 0
throw "#{num} is not positive"
num % 2 is 1
test = (v) ->
@cheng470
cheng470 / defaultArgument.coffee
Created October 10, 2014 06:16
定义默认参数
func = (c = d) ->
"hello #{c}"
func1 = (c) ->
c ?= d
"hello #{c}"
func2 = (c) ->
c = d unless c?
"hello #{c}"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="myId"></div>
<script id="jsbin-javascript">
alert(myId.id); // --> myId
@cheng470
cheng470 / tcp-server.js
Created October 10, 2014 10:27
一个服务器,它能够接受客户端的连接,并且在断开连接 前发送了一小段内容
var net = require('net');
var tcpServer = net.createServer();
tcpServer.on('connection', function(client) {
client.write('Hi!\n');
client.write('Bye!\n');
client.end();
});
clearArray1 = (arr) ->
arr.splice 0, arr.length # 返回被删除的部分,即原数组
clearArray2 = (arr) ->
arr.splice 0, arr.length
arr # 返回被清空的数组
clearArray3 = (arr) ->
arr.splice 0, arr.length
return # 单独使用return,什么都不返回
@cheng470
cheng470 / run.coffee
Last active August 29, 2015 14:07
写一个run函数,它接受一个函数作为第一个参数,然后个余下的参数都传递给被调用的函数。
run = (func, arg...) -> func arg...
###
生成的js代码:
var run,
__slice = [].slice;
run = function() {
@cheng470
cheng470 / escapeHtml.js
Created October 22, 2014 00:25
为了保证页面输出安全,我们经常需要对一些特殊的字符进行转义,请写一个函数escapeHtml,将<, >, &, “进行转义
function escapeHtml(str) {
return str.replace(/[<>"&]/g, function(match) {
switch (match) {
case "<": return "&lt;";
case ">": return "&gt;";
case "&": return "&amp;";
case "\"": return "&quot;";
}
});
}
@cheng470
cheng470 / run.html
Created October 22, 2014 04:37
通过弹窗运行代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>run code</title>
</head>
<body>
<textarea name="txt" id="txt" cols="40" rows="10"></textarea>
<br />
<input type="button" id="btn" value="Run">