Skip to content

Commit 62dcf30

Browse files
committed
implement bind for crappy browsers. add bind to distributions for bette
support. fix couple length var usages.
1 parent 185eb49 commit 62dcf30

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/core.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ var slice = Array.prototype.slice,
2828
return toString.call( arg ) === "[object Object]";
2929
};
3030

31+
// implement bind if browser doesn't natively support it
32+
if ( !Function.prototype.bind ) {
33+
Function.prototype.bind = function( obj ) {
34+
if(typeof this !== 'function') // closest thing possible to the ECMAScript 5 internal IsCallable function
35+
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
36+
37+
var slice = [].slice,
38+
args = slice.call(arguments, 1),
39+
self = this,
40+
nop = function () {},
41+
bound = function () {
42+
return self.apply( this instanceof nop ? this : ( obj || {} ),
43+
args.concat( slice.call(arguments) ) );
44+
};
45+
46+
bound.prototype = this.prototype;
47+
48+
return bound;
49+
};
50+
}
51+
3152
// global function
3253
function jStat() {
3354
return new jStat.fn.init( slice.call( arguments ));
@@ -73,7 +94,7 @@ jStat.fn.init.prototype = jStat.fn;
7394
// create method for easy extension
7495
jStat.extend = function( obj ) {
7596
var args = slice.call( arguments ),
76-
i = 1, j;
97+
i = 1, j;
7798
if ( args.length === 1 ) {
7899
for ( i in obj ) {
79100
jStat[i] = obj[i];
@@ -340,8 +361,8 @@ jStat.extend({
340361
arg = isArray( arg[0] ) ? arg : [ arg ];
341362

342363
// convert column to row vector
343-
var left = ( arr[0].length === 1 && arr[length] !== 1 ) ? jStat.transpose( arr ) : arr,
344-
right = ( arg[0].length === 1 && arg[length] !== 1 ) ? jStat.transpose( arg ) : arg,
364+
var left = ( arr[0].length === 1 && arr.length !== 1 ) ? jStat.transpose( arr ) : arr,
365+
right = ( arg[0].length === 1 && arg.length !== 1 ) ? jStat.transpose( arg ) : arg,
345366
res = [],
346367
row = 0,
347368
nrow = left.length,
@@ -377,7 +398,7 @@ jStat.extend({
377398
// computes the norm of the vector
378399
norm : function( arr ) {
379400
arr = isArray( arr[0] ) ? arr : [ arr ];
380-
if( arr.length > 1 && arr[0][length] > 1 ) {
401+
if( arr.length > 1 && arr[0].length > 1 ) {
381402
// matrix norm
382403
} else {
383404
// vector norm
@@ -396,7 +417,7 @@ jStat.extend({
396417
var issymmetric = true,
397418
row = 0,
398419
size = arr.length, col;
399-
if( arr.length !== arr[0][length] ) return false;
420+
if( arr.length !== arr[0].length ) return false;
400421
for ( ; row < size; row++ ) {
401422
for ( col = 0; col < size; col++ ) {
402423
if ( arr[col][row] !== arr[row][col] ) return false;

src/distribution.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ jStat.beta = function( alpha, beta ) {
55
if (!( this instanceof arguments.callee )) return new jStat.beta( alpha, beta );
66
this.alpha = alpha;
77
this.beta = beta;
8+
9+
for ( var i in jStat.beta.prototype ) this[i] = this[i].bind( this );
810
};
911

1012
// extend beta function with static methods
@@ -76,6 +78,8 @@ jStat.cauchy = function( local, scale ) {
7678
if (!( this instanceof arguments.callee )) return new jStat.cauchy( local, scale );
7779
this.local = local;
7880
this.scale = scale;
81+
82+
for ( var i in jStat.cauchy.prototype ) this[i] = this[i].bind( this );
7983
};
8084

8185
// extend cauchy function with static methods
@@ -144,6 +148,8 @@ jStat.cauchy.prototype.sample = function( arr ) {
144148
jStat.chisquare = function( dof ) {
145149
if (!( this instanceof arguments.callee )) return new jStat.chisquare( dof );
146150
this.dof = dof;
151+
152+
for ( var i in jStat.chisquare.prototype ) this[i] = this[i].bind( this );
147153
};
148154

149155
// extend chisquare function with static methods
@@ -213,6 +219,8 @@ jStat.chisquare.prototype.sample = function( arr ) {
213219
jStat.exponential = function( rate ) {
214220
if (!( this instanceof arguments.callee )) return new jStat.exponential( rate );
215221
this.rate = rate;
222+
223+
for ( var i in jStat.exponential.prototype ) this[i] = this[i].bind( this );
216224
};
217225

218226
// extend exponential function with static methods
@@ -282,6 +290,8 @@ jStat.gamma = function( shape, scale ) {
282290
if (!( this instanceof arguments.callee )) return new jStat.gamma( shape, scale );
283291
this.shape = shape;
284292
this.scale = scale;
293+
294+
for ( var i in jStat.gamma.prototype ) this[i] = this[i].bind( this );
285295
};
286296

287297
// extend gamma function with static methods
@@ -348,6 +358,8 @@ jStat.kumaraswamy = function( alpha, beta ) {
348358
if (!( this instanceof arguments.callee )) return new jStat.kumaraswamy( alpha, beta );
349359
this.alpha = alpha;
350360
this.beta = beta;
361+
362+
for ( var i in jStat.kumaraswamy.prototype ) this[i] = this[i].bind( this );
351363
};
352364

353365
// extend kumaraswamy function with static methods
@@ -399,6 +411,8 @@ jStat.lognormal = function( mu, sigma ) {
399411
if (!( this instanceof arguments.callee )) return new jStat.lognormal( mu, sigma );
400412
this.mu = mu;
401413
this.sigma = sigma;
414+
415+
for ( var i in jStat.lognormal.prototype ) this[i] = this[i].bind( this );
402416
};
403417

404418
// extend lognormal function with static methods
@@ -468,6 +482,8 @@ jStat.normal = function( mean, std ) {
468482
if (!( this instanceof arguments.callee )) return new jStat.normal( mean, std );
469483
this.mean = mean;
470484
this.std = std;
485+
486+
for ( var i in jStat.normal.prototype ) this[i] = this[i].bind( this );
471487
};
472488

473489
// extend normal function with static methods
@@ -537,6 +553,8 @@ jStat.pareto = function( scale, shape ) {
537553
if (!( this instanceof arguments.callee )) return new jStat.pareto( scale, shape );
538554
this.scale = scale;
539555
this.shape = shape;
556+
557+
for ( var i in jStat.pareto.prototype ) this[i] = this[i].bind( this );
540558
};
541559

542560
// extend pareto function with static methods
@@ -588,6 +606,8 @@ jStat.extend( jStat.pareto, {
588606
jStat.studentt = function( dof ) {
589607
if (!( this instanceof arguments.callee )) return new jStat.studentt( dof );
590608
this.dof = dof;
609+
610+
for ( var i in jStat.studentt.prototype ) this[i] = this[i].bind( this );
591611
};
592612

593613
// extend studentt function with static methods
@@ -660,6 +680,8 @@ jStat.weibull = function( scale, shape ) {
660680
if (!( this instanceof arguments.callee )) return new jStat.weibull( scale, shape );
661681
this.scale = scale;
662682
this.shape = shape;
683+
684+
for ( var i in jStat.weibull.prototype ) this[i] = this[i].bind( this );
663685
};
664686

665687
// extend weibull function with static methods
@@ -729,6 +751,8 @@ jStat.uniform = function( a, b ) {
729751
if (!( this instanceof arguments.callee )) return new jStat.uniform( a, b );
730752
this.a = a;
731753
this.b = b;
754+
755+
for ( var i in jStat.uniform.prototype ) this[i] = this[i].bind( this );
732756
};
733757

734758
// extend uniform function with static methods

0 commit comments

Comments
 (0)