Last active
September 3, 2015 08:42
-
-
Save milingo/f795fffe43d1ceca8f7b to your computer and use it in GitHub Desktop.
javascript lessons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var groups =_.groupBy(categories, _.curry(_.has)(_, 'id')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Root class | |
var ElasticQuery = function () { | |
}; | |
module.exports = ElasticQuery; | |
// Subclass 1 | |
var ElasticQuery = require('./elasticQuery'); | |
var ElasticReportQuery = function () { | |
this.excludeInvalidEvents(); | |
this.aggs = { | |
"cost": { | |
"sum": { "field": "cost" } | |
}, | |
"type": { | |
"terms": { "field": "typeName"} | |
} | |
} | |
this.aggs.unique_users = this.getUniqueUsersQuery(); | |
return this; | |
}; | |
ElasticReportQuery.prototype = new ElasticQuery(); | |
module.exports = ElasticReportQuery; | |
// Subclass 2 | |
var ElasticQuery = require('./elasticQuery'); | |
var ElasticEventQuery = function () { | |
this.excludeInvalidEvents(); | |
return this; | |
}; | |
ElasticEventQuery.prototype = new ElasticQuery(); | |
module.exports = ElasticEventQuery; | |
// Another way in node | |
var CampaignConfigController = function () { | |
// ... | |
} | |
CampaignConfigController.prototype.aFunction = function() { | |
// ... | |
} | |
util.inherits(CampaignConfigController, BaseController); | |
module.exports = CampaignConfigController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of a enum-like class in javascript | |
var WeekDays = function () { | |
// This could be regarded as a constructor | |
// We cannot fit this into the prototype since "elements2 would be shared accross different instances | |
this.elements = { | |
MONDAY: {name: "mon", index: 1}, | |
TUESDAY: {name: "tue", index: 2}, | |
WEDNESDAY: {name: "wed", index: 3}, | |
THURSDAY: {name: "thu", index: 4}, | |
FRIDAY: {name: "fri", index: 5}, | |
SATURDAY: {name: "sat", index: 6}, | |
SUNDAY: {name: "sun", index: 7} | |
} | |
}; | |
WeekDays.prototype = { | |
// This is the interface of the class | |
nameFromIndex: function (index) { | |
return this.elements[index].name; | |
}, | |
wholeWeekInexes: function () { | |
return this.elements.map(function (day) { | |
return day.index; | |
}); | |
} | |
} | |
module.exports = WeekDays; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment