Skip to content

Instantly share code, notes, and snippets.

var _addProps = function(a, name)
{
a[name + '.a'] = name + '.a';
a['d'] = name + '.d';
};
var _create = function(proto)
{
var C = function(){};
C.prototype = proto;
@bga
bga / Function bind.js
Created February 16, 2011 07:43
Function bind.js
/*
uses compilation to optimize `Function#bind`, but not real `bind` which in fact - `carry`
inlines consts.
*/
var defaultThis = (function(){ return this })();
this._fastBind = (function()
{
var _quoteString = function(s)
// yield in es3
// original code
(function()
{
var _gen = function(a)
{
var d = '';
for(;;) {
/*
Shows that strongly typed vars is possible in js! And do not worry about speed. :) Robust type system is more important than yet another benchmark :P. And i'll plan to make deploy system which checks all types and removes all 'ugly' `with`. This code definitely should be complexer, but i keep it simple.
P.S. `with` rulezzz
P.P.S. As you see its too easy to make field of class strongly typed too.
*/
(function($G){
if(Object.defineProperty && !{}.__defineGetter__)
{
Object.prototype.__defineGetter__ = function(name, _fn)
/*
my OOP in js is more and more closer to java/c++/c# :P
1) now it allows create instance w/o <new> keyword (all native classes supports instance creating w/o <new> except <Date>)
2) removes <prototype> extra word when you access to static fields and methods(<Class.prototype = Class> magic)
3) automatic delegates/<Function#bind>. My delegates always is begined from '_on' prefix
4) has own implementation of <instanceof> operator - <Object#_hasBaseClass>
5) base constructors call via special <Object#_callBaseConstructor>
6) derivation do not uses prototype machanism(because slow resolving). I use just copy _methods/InternalTypes/CONSTS. Yes its based on codestyle. But you can use <typeof> or <Object#toString> instead.
*/
/*
Shows that Mutexes, Events, and Critical Sections are available in js "threading" model too. :)
WaitForSingleObject http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspx
WaitForMultipleObjects http://msdn.microsoft.com/en-us/library/ms687025(VS.85).aspx
Event http://msdn.microsoft.com/en-us/library/ms682396(VS.85).aspx
*/
(function($G)
{
/*
unfortunately you can not debug it :(
*/
(function($G)
{
$G._import = function(_fn, modules)
{
var argsString = '';
var args = [];
// from https://gist.github.com/613924
Proxy._noopHandler = function noopHandler(obj) {
return {
getOwnPropertyDescriptor: function(name) {
var desc = Object.getOwnPropertyDescriptor(obj, name);
// a trapping proxy's properties must always be configurable
desc.configurable = true;
return desc;
},
getPropertyDescriptor: function(name) {
var a = [x*2 for each (x in [1,2,3])];
var a = [1,2,3].map(_("x -> 2*x"));
var b = [x*2 for each (x in [1,2,3]) if (x>1)];
var a = [1,2,3].filter(_("x -> x > 1")).map(_("x -> 2*x"));
var c = [x+y
for each (x in [1,2,3])
for each (y in [10,20,30])
if (x>1)
/*
# Deflate compression using Canvas 2d API
Canvas API provides HTMLCanvasElement#toDataURL, which allows serialize canvas image to png image. I do not know about result png format (how many bits per pixel and there is alpha channel or not, may be palette or grayscale image). WHATWG spec also do not points this ie its UA dependent. Anyway basic rules:
+ browsers do not supports images with width or height > 20 000 px => we use square image instead linear(1*X or X*1)
+ if alpha component of canvas data eq 0 => browser reset red, green, blue to 0 too even if globalCompositeOperation = 'copy'
+ even if globalCompositeOperation = 'copy', globalAlpha = 1.0, dest alpha of pixel eq 255 - browser blends src image data when you perform putImageData and original red, green, blue components corrupts due floating point errors. For example we have one pixel [1,129,130,131], let see putImageData(this pixel); getImageData() -> [1,128,130,131]; drawImage(toDataUrl()); ; getImageData -> [1,126,128,131] =>