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 / run.coffee
Last active August 29, 2015 14:07
写一个run函数,它接受一个函数作为第一个参数,然后个余下的参数都传递给被调用的函数。
run = (func, arg...) -> func arg...
###
生成的js代码:
var run,
__slice = [].slice;
run = function() {
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 / 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();
});
<!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 / 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}"
@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 / 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 / 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 / css-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 / css-two-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 {