Skip to content

Instantly share code, notes, and snippets.

View zdying's full-sized avatar

zhang zdying

View GitHub Profile
function debugAccess(obj, prop, debugGet){
var origValue = obj[prop];
Object.defineProperty(obj, prop, {
get: function () {
if ( debugGet )
debugger;
return origValue;
},
/**
* Function.prototype.bind
*/
if (!isFunc(Function.prototype.bind)){
Function.prototype.bind = function(thisArg){
if(!isFunc(this)){
throw new TypeError("Bind must be called on a function")
}
var slice = [].slice,
/**
* Object.create
* from : http://javascript.crockford.com/prototypal.html
*/
if (!isFunc(Object.create)){
Object.create = function(proto){
function F(){}
F.prototype = proto;
return new F();
}
/**
* Math.guid
* from : http://www.broofa.com/2008/09/javascript-uuid-function/
*/
Math.guid = function(){
return 'xxxxxxxx-xxxx-4xxx-yxxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c){
var r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16)
@zdying
zdying / JavaScript inheritance.js
Created August 14, 2014 05:35
javascript inheritance
function Person(name){
this.name = name;
}
Person.prototype.sayName = function(){
console.log(this.name);
}
function extend(subClass, superClass){
function F(){}
@zdying
zdying / highlight.js
Created August 14, 2014 05:38
simple highlighter
function highlight(str){
var colors = ['blue','rgb(9, 197, 9)','red','lightgray'];
var reg = /(var|this|new|return|function)|([\"'].*?[\"'])|([+-]?\d+(?:\.\d+)?)|(\/\/.*|\/\*[\s\S]*?\*\/)/g;
var res = str.replace(reg, function(match){
var args = [].slice.call(arguments,1);
return '<span style="color:' + colors[args.indexOf(match)] + '">' + match + '</span>';
});
return res;
}
@zdying
zdying / proto-tree.js
Last active August 29, 2015 14:05
遍历对象的属性,包括原型属性
/**
* 遍历对象的属性,包括原型属性
*/
var obj = {};
obj.a = {
'a1' : 'a',
'b1' : {
'aa' : 'aa',
'bb' : {
'aaa' : 'aaa',
@zdying
zdying / globalEval.js
Created August 25, 2014 05:47
Evaluates a script in a global context
// from jQuery
// Evaluates a script in a global context
globalEval: function( code ) {
var script = document.createElement( "script" );
script.text = code;
document.head.appendChild( script ).parentNode.removeChild( script );
}
@zdying
zdying / 两拦布局右侧宽度固定-左侧自适应.html
Created October 15, 2014 02:00
两拦布局右侧宽度固定-左侧自适应
<html>
<head>
<title>两拦布局右侧宽度固定-左侧自适应</title>
<style media="screen">
.con{width: 90%; overflow:hidden; border:1px dashed red}
.right{float: right; width:200px; margin-left:-200px; height:400px; background: green}
.left{float:left; width:100%;}
.main{margin-right:200px; height:500px; background: orange}
</style>
</head>
@zdying
zdying / pathAnimate.swift
Created December 1, 2014 05:26
Path Animate
//
// ViewController.swift
// test
//
// Created by 张代应 on 14/11/28.
// Copyright (c) 2014年 imf2e. All rights reserved.
//
import UIKit
//import CoreText