Skip to content

Instantly share code, notes, and snippets.

View lylijincheng's full-sized avatar

lylijincheng lylijincheng

View GitHub Profile
@lylijincheng
lylijincheng / ee.js
Created August 7, 2013 11:07
Backbone events emitter.
/**
* Example1:
* var Person, man;
* Person = Klass.create();
* Person.implement(EventEmitter);
* man = Person.instance();
* man.on('say', function(what) { alert(what); });
* man.trigger('say', 'balabala...');
*
* Example2:
@lylijincheng
lylijincheng / extend.js
Created August 7, 2013 10:58
coffee extend
var __hasProp, __extends;
__hasProp = {}.hasOwnProperty,
__extends = function (child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor() {
this.constructor = child;
}
@lylijincheng
lylijincheng / ri20min.rb
Created August 7, 2013 10:57
ruby in 20minutes
#!/usr/bin/env ruby
class MegaGreeter
attr_accessor :names
# Create the object
def initialize(names = "World")
@names = names
end
@lylijincheng
lylijincheng / this
Last active December 20, 2015 17:59
this in javascript.
// jqia
对象的本质是一个无序的 “名/值” 对应关系的集合,名称是字符串,值可以是任何Javascript对象。
// this
在基于面向对象的语言里,`this`参数通常引用类的实例(方法在类中声明)。在Javascript中函数也是对象,
`this`所引用的对象被称为函数上下文,它不是由如何声明函数,而是由如何调用函数决定的。意味着更具函数
如何被调用,同一函数可以有不同的上下文。在默认情况下,函数调用的上下文(this)是对象,其属性包含可
以调用该函数的引用。
@lylijincheng
lylijincheng / prototype.js
Last active December 20, 2015 17:59
`prototype` vs `__proto__`
// Prototypal Inheritatance Definition:
// When accessing the propertyies of an object, Javascript will traverse the prototype chain
// upwards until finds a property with the requested name.
// Most Javascript implementations use __proto__ property to represent the next object in the
// prototype chain.
// Note: __proto__ is non-standard and should not be used in your code.
@lylijincheng
lylijincheng / object.js
Last active December 20, 2015 17:59
ES5 Object define properties
// Create an object with "No Properties"
var person = Object.create(null);
Object.defineProperty(person, 'fisrtName', {
value: "Jin Cheng",
writable: true,
enumerable: true,
configurable: true
});
@lylijincheng
lylijincheng / viewport.js
Created August 7, 2013 10:30
Safari on iOS viewport
// Safari on iOS viewport
// Executed at window onload.
this.viewport = function() {
var width, height, screenWidth, screenHeight, innerWidth, innerHeight, isSafariOnIOS, orientation, userAgent;
screenWidth = window.screen.width;
screenHeight = window.screen.height;
innerWidth = window.innerWidth;