Skip to content

Instantly share code, notes, and snippets.

View z2015's full-sized avatar
🎯
Focusing

William Zhou z2015

🎯
Focusing
View GitHub Profile
@z2015
z2015 / close.css
Created May 16, 2016 03:23
pure css text close button
/* Common */
body {
color: #777;
text-align: center;
}
/* Close Button */
[class*='close-'] {
@z2015
z2015 / Decorator_1.js
Last active September 21, 2022 18:31
The Decorator Pattern
// A vehicle constructor
function Vehicle( vehicleType ){
// some sane defaults
this.vehicleType = vehicleType || "car";
this.model = "default";
this.license = "00000-000";
}
/**
Code copyright Dustin Diaz and Ross Harmes, Pro JavaScript Design Patterns.
**/
// Constructor.
var Interface = function (name, methods) {
if (arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length + "arguments, but expected exactly 2.");
}
this.name = name;
@z2015
z2015 / Mixins.js
Created May 12, 2016 09:31
Mixins
var myMixins = {
moveUp: function(){
console.log( "move up" );
},
moveDown: function(){
console.log( "move down" );
},
@z2015
z2015 / Subclassing.js
Created May 12, 2016 09:28
Sub-classing
var Person = function( firstName, lastName ){
this.firstName = firstName;
this.lastName = lastName;
this.gender = "male";
};
// a new instance of Person can then easily be created as follows:
var clark = new Person( "Clark", "Kent" );
@z2015
z2015 / Factory.js
Created May 12, 2016 07:26
Factory Pattern
// Types.js - Constructors used behind the scenes
// A constructor for defining new cars
function Car( options ) {
// some defaults
this.doors = options.doors || 4;
this.state = options.state || "brand new";
this.color = options.color || "silver";
@z2015
z2015 / Facade.js
Created May 12, 2016 07:02
Facade Pattern
var module = (function() {
var _private = {
i: 5,
get: function() {
console.log( "current value:" + this.i);
},
set: function( val ) {
this.i = val;
},
@z2015
z2015 / command.js
Created May 12, 2016 06:43
Command Pattern
carManager.execute = function ( name ) {
return carManager[name] && carManager[name].apply( carManager, [].slice.call(arguments, 1) );
};
carManager.execute( "arrangeViewing", "Ferrari", "14523" );
carManager.execute( "requestInfo", "Ford Mondeo", "54323" );
carManager.execute( "requestInfo", "Ford Escort", "34232" );
carManager.execute( "buyVehicle", "Ford Escort", "34232" );
@z2015
z2015 / Prototype.js
Created May 12, 2016 05:57
Prototype pattern
var beget = (function () {
function F() {}
return function ( proto ) {
F.prototype = proto;
return new F();
};
})();
@z2015
z2015 / float.md
Created May 12, 2016 05:46
ceil vs floor vs round

The Math.ceil() function returns the smallest integer greater than or equal to a given number. Math.ceil(.95); // 1 Math.ceil(4); // 4 Math.ceil(7.004); // 8

The Math.floor() function returns the largest integer less than or equal to a given number. Math.floor( 45.95); // 45 Math.floor(-45.95); // -46

The Math.round() function returns the value of a number rounded to the nearest integer.