In the javascript, is this function pure - javascript

function add() {
var args = [].slice.call(arguments);
var fn = function() {
var fn_args = [].slice.call(arguments)
return add.apply(null, args.concat(fn_args))
}
fn.toString = function() {
return '' + args.reduce(function(acc, prev){
return acc + prev
})
}
return fn;
}
I don't sure if this function is pure.
because I can change the result externally;
var result = add(1);
result.toString = () => 'hello';

Related

Pure Javascript : Override functions

I would like to override some functions (for logging some informations)
I'm trying to do something like:
function universe() {
return 42;
}
universe = universe.override(function(){
console.log("Calling universe");
return this.$super();
});
Full sample:
Function.prototype.override = function (fn) {
var $super = this;
var f = function overrided() {
var context = this || $super || {};
context.$super = $super;
return fn.apply(context, arguments);
};
f.$super = $super;
return f;
};
Function.prototype.unoverride = function () {
if (this.$super) {
return this.$super;
}
return this;
};
function universe() {
return 42;
}
function mulBy10() {
console.warn("calling overrided function");
return this.$super() * 10;
}
console.log("---------");
console.log("original:", universe());
universe = universe.override(mulBy10);
console.log("new one:", universe());
universe = universe.unoverride();
console.log("reverted:", universe());
console.log("--With Object");
var MyObject = function() {
this.value = 42;
}
MyObject.prototype = {
constructor: MyObject,
getValue: function() {
return this.value;
}
};
var o1 = new MyObject();
console.log("MyObject original:", o1.getValue());
o1.getValue = o1.getValue.override(mulBy10);
console.log("MyObject new one:", o1.getValue());
o1.getValue = o1.getValue.unoverride();
console.log("MyObject reverted:", o1.getValue());
console.log("--With Object prototype");
o2 = new MyObject();
MyObject.prototype.getValue = MyObject.prototype.getValue.override(mulBy10);
console.log("MyObject.proto new one:", o2.getValue());
MyObject.prototype.getValue = MyObject.prototype.getValue.unoverride();
console.log("MyObject.proto reverted:", o2.getValue());
console.log("--With recursive");
function recur(it, max) {
console.log("it:", it, "max:", max);
if( it >= max ) {
console.log("finished");
return;
}
recur(it + 1, max);
}
recur = recur.override(function(it, max){
console.warn("Overrided recur");
return this.$super(it, max);
});
recur(0, 4);
This works fine with function, object functions.
But it doesn't work when i try to override CasperJs "require" function.
I did:
require = require.override(function(file){
console.log("require(" + file + ")");
return this.$super(file);
});
So i was wondering, in which case, override function will not work ?
Did i missed something in CasperJS require function ?

JavaScript way to define variable

Hello I saw "this" (now I just simplified it) code on a website source.
The question is:
Why is this._position defined with m(p) instead of just p ?
does it have some logical explanation ?
var emptyFunction = function j() {};
emptyFunction.thatReturnsValue = function(j) {
return j;
};
var m = emptyFunction.thatReturnsValue;
function o(){
this._position = 'left';
}
o.prototype.setPosition = function(p) {
'use strict';
this._position = m(p);
return this;
};
o.prototype.getPosition = function(){
return this._position;
}
function calculatePosition(oInst){
var position;
//( do some math to figure out the best position)
position = 'right';
oInst.setPosition(position);
}
function realWork(){
var orientation = new o();
calculatePosition(orientation);
console.log(orientation.getPosition());
}
empty function is used for more things:
**
function h(j) {
return function() {
return j;
};
}
var emptyFunction = function j() {};
emptyFunction.thatReturns = h;
emptyFunction.thatReturnsFalse = h(false);
emptyFunction.thatReturnsTrue = h(true);
emptyFunction.thatReturnsNull = h(null);
emptyFunction.thatReturnsThis = function() {
return this;
};
emptyFunction.thatReturnsValue = function(j) {
return j;
};
**

How to Write JavaScript Object like that

How to write an Object for using this object like below
var cal = new calculator;
cal.add(10).add(20).miniz(2).div(2);
console.log(cal.result()); // result 14
Here you go, this is one way to do it:
My Example
var calculator = function() {
this.curr = 0;
this.add = function(n) {
this.curr += n;
return this; // returning this at the end of each method is the key to chaining
};
this.miniz = function(n) {
this.curr -= n;
return this;
};
this.div = function(n) {
this.curr = this.curr / n;
return this;
};
this.result = function() {
return this.curr;
};
};
You need to change the instantiation to this:
var cal = new calculator();
Just to get you started:
function Calculator() {
var value = 0;
this.add = function (v) {
value += v;
return this;
};
this.result = function () {
return value;
};
}
var cal = new Calculator;
console.log(cal.add(10).result()); // result 10
may be this is will help some what..
var Calc = function(){
this.value = 0;
};
Calc.prototype.add = function(val){
this.value += val;
return this;
};
then you can use like new Calc().add(100).add(100)
but before make sure understood how prototyping is working,
for ref : a sample
function calculator(){
this.val = 0;
this.add = function(num){
this.val += num;
return this;
};
this.miniz = function(num){
this.val -= num;
return this;
};
this.div = function(num){
this.val /= num;
return this;
};
this.result = function(){
return this.val;
};
}

Set value into object with function (java script)

When I used getFullName, getFirstName and getLastName work ok, but I can't use set functions setFullName, setLastName, setFirstName. My code:
var Person = function(firstAndLast) {
var fn=firstAndLast.split(' ');
var fstr=fn.join(' ');
var frn=fn[0];
var lsn=fn[1];
this.getFullName=function(){return fstr;};
this.getFirstName=function(){return frn;};
this.getLastName=function(){return lsn;};
this.setFirstName=function(a){fn[0]=a;};
this.setLastName=function(b){fn[1]=b;};
this.setFullName=function(c){fn=c.split(' ');};
};
What about this:
var Person = function(firstAndLast) {
var self = this;
this.fn = firstAndLast.split(' ');
this.frn = this.fn[0];
this.lsn = this.fn[1];
this.getFullName=function(){return self.fn.join(' ');};
this.getFirstName=function(){return self.frn;};
this.getLastName=function(){return self.lsn;};
this.setFirstName=function(a){self.frn=a; self.fn[0]=a;};
this.setLastName=function(b){self.lsn=b; self.fn[1]=b;};
this.setFullName=function(c){
self.fn = c.split(' ');
self.frn = this.fn[0];
self.lsn = this.fn[1];};
};
See this fiddle
If you have a lot of Person objects, you should consider moving the getter/setter functions to the class prototype:
var Person = function(firstAndLast) {
this.fn = firstAndLast.split(' ');
this.frn = this.fn[0];
this.lsn = this.fn[1];
};
Person.prototype.getFullName = function() {
return this.fn.join(' ');
}
Person.prototype.getFirstName = function() {
return this.lsn;
}
Person.prototype.getLastName = function() {
return this.lsn;
}
Person.prototype.setFirstName = function(a) {
this.frn=a;
this.fn[0]=a;
}
Person.prototype.setLastName = function(b) {
this.lsn=b;
this.fn[1]=b;
}
Person.prototype.setFullName = function(c) {
this.fn = c.split(' ');
this.frn = this.fn[0];
this.lsn = this.fn[1];
}
See updated fiddle

Running multiple javascript object methods together

I am trying to write a little helper class for my ajax chat system i am working on just trying to add basic functions that i may need.
var strings = {
filterWords: ["fool", "dumb", "arse"],
removeSpecialChars: function (str) {
return str.replace(/[^\w\s]/gi, '');
},
killSpace: function (str) {
return str.replace(/\s/g, '');
},
reduceSpace: function (str) {
return str.replace(/\s+/g, ' ');
},
allowLetsAndNums: function (str) {
return str.replace(/[^A-Za-z0-9]/g, ' ');
},
allowLets: function (str) {
return str.replace(/[^A-Za-z]/g, ' ');
},
allowNums: function (str) {
return str.replace(/[^0-9]/g, ' ');
},
wordFilter: function (str) {
var rgx = new RegExp(this.filterWords.join("|"), "gi");
return str.replace(rgx, "****");
}
}
What i am finding is i may need to run multiple methods together i am asking whats the best practise to do this without resulting with below?
alert(strings.wordFilter(strings.reduceSpace(strings.allowLets("efgwge #£235%^#£ fool you a dumb arse432345$%^"))));
Thanks
You could make this a fluent interface, allowing code like this:
var x = new Validation("efgwge #£235%^#£ fool you a dumb arse432345$%^");
alert(x.allowLets().reduceSpace().wordFilter().result());
// alerts "efgwge **** you a **** ****"
Your main code would need to be:
var Validation = function(str) {
this.str = str;
filterWords = ["fool", "dumb", "arse"]
this.removeSpecialChars = function () {
this.str = this.str.replace(/[^\w\s]/gi, '');
return this;
};
this.killSpace = function () {
this.str = this.str.replace(/\s/g, '');
return this;
};
this.reduceSpace = function () {
this.str = this.str.replace(/\s+/g, ' ');
return this;
};
this.allowLetsAndNums = function () {
this.str = this.str.replace(/[^A-Za-z0-9]/g, ' ');
return this;
};
this.allowLets = function () {
this.str = this.str.replace(/[^A-Za-z]/g, ' ');
return this;
};
this.allowNums = function () {
this.str = this.str.replace(/[^0-9]/g, ' ');
return this;
};
this.wordFilter = function () {
var rgx = new RegExp(filterWords.join("|"), "gi");
this.str = this.str.replace(rgx, "****");
return this;
};
this.result = function(){
return this.str;
};
}
Live example: http://jsfiddle.net/fb7en/
You could extend the String prototype:
String.prototype.removeSpecialChars = function () {
return this.replace(/[^\w\s]/gi, '');
}
String.prototype.killSpace = function () {
return this.replace(/\s/g, '');
}
var foo = "This is my§$% String";
​document.write​(foo.removeSpecialChars​().killSpace());​
You could add the functions to the String.prototype so you can call the functions like this:
String.prototype.killSpace = function() {
return this.replace(/\s/g, '');
}
String.prototype.reduceSpace = function () {
return this.replace(/\s+/g, ' ');
}
"foo bar".reduceSpace().killSpace(); // => returns foobar
Only downside to this is that you can't iterate over a string with a for..in loop then because it will list the method as a member and there's currently no cross-browser way to make it non-iterable (IE doesn't support it).
You might consider a chainable API for your Object:
var StringFilter = {
_string: '',
string: function (string) {
this._string = string || '';
return this;
},
filterWords: ["fool", "dumb", "arse"],
removeSpecialChars: function () {
this._string = this._string.replace(/[^\w\s]/gi, '');
return this;
},
killSpace: function () {
this._string = this._string.replace(/\s/g, '');
return this;
},
reduceSpace: function () {
this._string = this._string.replace(/\s+/g, ' ');
return this;
},
allowLetsAndNums: function () {
this._string = this._string.replace(/[^A-Za-z0-9]/g, ' ');
return this;
},
allowLets: function () {
this._string = this._string.replace(/[^A-Za-z]/g, ' ');
return this;
},
allowNums: function () {
this._string = this._string.replace(/[^0-9]/g, ' ');
return this;
},
wordFilter: function () {
var rgx = new RegExp(this.filterWords.join("|"), "gi");
this._string = this._string.replace(rgx, "****");
return this;
},
select: function () {
return this._string;
}
};
StringFilter
.string("efgwge #£235%^#£ fool you a dumb arse432345$%^")
.allowLets()
.reduceSpace()
.wordFilter()
.select();

Categories

Resources