JavaScript Proprietary Library Example: Difference between revisions
Jump to navigation
Jump to search
Line 158: | Line 158: | ||
$class : 'CKi.Edu.YouTube.Channel', | $class : 'CKi.Edu.YouTube.Channel', | ||
$meta : function(){return $.meta(me)}, | $meta : function(){return $.meta(me)}, | ||
getIndex : function(name, index, channel){ | |||
for(index=0;index < channels.length;index++){ | |||
channel = channels[index]; | |||
if(channel.name.toLowerCase() == name.toLowerCase()){ | |||
return index; | |||
} | |||
} | |||
return -1; | |||
}, | |||
isExist : function(name, index){ | |||
index = me.getIndex(name); | |||
return index >= 0; | |||
}, | |||
add : function(name, lang, liked){ | add : function(name, lang, liked){ | ||
channels.push({name: name, lang: lang||'English', like: liked||0}); | if(!me.isExist(name)){ | ||
channels.push({name: name, lang: lang||'English', like: liked||0}); | |||
} else {'"{0}" Already Exist!'.format(name).warn();} | |||
return me; | return me; | ||
}, | }, | ||
del : function(name, index){ | |||
if(me.isExist(name)){ | |||
index = me.getIndex(name); | |||
channels.splice(index, 1); | |||
return me; | |||
} else {'"{0}" Not Yet Found!'.format(name).warn();} | |||
return me; | return me; | ||
}, | }, |
Revision as of 08:21, 19 January 2019
Javascript is pretty cool programming language ever. Easy to read and write. Most of the beginner unable to understand, read, write OOP in Javascript. Where professional are fluent in OOP Javascript. Most of the professional have to relay customized Javascript library on top of others Open Source Javascript framework. Here is example for beginner to professional those are interested to write OOP in Javascript.
Prototyping Library
/**
* This is the Number.lpad() method developed aim to support
* oracle like lpad single rows function for JS Engine.
*
* @public
* @package Number
* @class Number.lpad()
*/
;Number.prototype.lpad||(function(){
Number.prototype.lpad = function(width, fill, me, padded) {
me = this.toString();
if(me.length >= width){
return me;
}
padded = new Array(width - me.length + 1);
return padded.join(fill||'0') + me;
};
})();
/**
* This is the String.format() method developed aim to support
* Java like format String in perspectives of JS Engine.
*
* @public
* @package String
* @class String.format()
*/
;String.prototype.format||(function(){
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
})();
/**
* This is the String.info() method developed aim to support
* Java like print String in perspectives of JS Engine.
*
* @public
* @package String
* @class String.info()
*/
;String.prototype.info||(function(){
String.prototype.info = function() {
if(console && console.info){
console.info(this.toString());
}
return this;
};
})();
/**
* This is the String.rpad() method developed aim to support
* Oracle like rpad single rows function for JS Engine.
*
* @public
* @package String
* @class String.rpad()
*/
;String.prototype.rpad||(function(){
String.prototype.rpad = function(width, fill, me, padded) {
me = this;
if(me.length >= width){
return me;
}
padded = new Array(width - me.length + 1);
return me + padded.join(fill||'0');
};
})();
/**
* This is the String.warn() method developed aim to support
* Java like print String in perspectives of JS Engine.
*
* @public
* @package String
* @class String.warn()
*/
;String.prototype.warn||(function(){
String.prototype.warn = function() {
if(console && console.warn){
console.warn(this.toString());
}
return this;
};
})();
Proprietary Library
/**
* @public
* @class Chorke
* @package Default
*/
var Chorke=Chorke||(function(me){
me = {
$class : 'Chorke',
$package : 'Default',
meta : function(ns, tpl, out){
tpl = '{\n "package" : "{0}",\n "class" : "{1}"\n}';
out = tpl.format(ns.$package, ns.$class);
return out;
}
};
return me;
})(),
/**
* @public
* @class CKi
* @package Default
*/
CKi=CKi||(function(me){
me.$class = 'CKi';
me.$meta = function(){return me.meta(me)};
return me;
})(Chorke);
/**
* @public
* @static
* @package CKi
* @class CKi.Edu
*/
;CKi.Edu||(function($, me){
me = {
$package : 'CKi',
$class : 'CKi.Edu',
$meta : function(){return $.meta(me)}
}
$.Edu = me;
})(CKi);
/**
* @public
* @static
* @package CKi.Edu
* @class CKi.Edu.YouTube
*/
;CKi.Edu.YouTube||(function($, me){
me = {
$package : 'CKi.Edu',
$class : 'CKi.Edu.YouTube',
$meta : function(){return $.meta(me)}
}
$.Edu.YouTube = me;
})(CKi);
/**
* @public
* @static
* @package CKi.Edu.YouTube
* @class CKi.Edu.YouTube.Channel
*/
;CKi.Edu.YouTube.Channel||(function($, me, channels){
channels = [];
me = {
$package : 'CKi.Edu.YouTube',
$class : 'CKi.Edu.YouTube.Channel',
$meta : function(){return $.meta(me)},
getIndex : function(name, index, channel){
for(index=0;index < channels.length;index++){
channel = channels[index];
if(channel.name.toLowerCase() == name.toLowerCase()){
return index;
}
}
return -1;
},
isExist : function(name, index){
index = me.getIndex(name);
return index >= 0;
},
add : function(name, lang, liked){
if(!me.isExist(name)){
channels.push({name: name, lang: lang||'English', like: liked||0});
} else {'"{0}" Already Exist!'.format(name).warn();}
return me;
},
del : function(name, index){
if(me.isExist(name)){
index = me.getIndex(name);
channels.splice(index, 1);
return me;
} else {'"{0}" Not Yet Found!'.format(name).warn();}
return me;
},
compare : function(prev, next) {
if (prev.name < next.name){
return -1;
} else if (prev.name > next.name){
return 1;
}
return 0;
},
sort : function(){
return channels.sort(me.compare);
},
format : function(index, channel, name, lang, like, tpl, out){
tpl = ' {0}. Chorke {1}, Inc. => {2}[{3}]\n';
out = '\n';me.sort();
for(index=0;index < channels.length;index++){
channel = channels[index];
name = channel.name;
lang = channel.lang;
like = channel.like;
out += tpl.format((index + 1).lpad(2), name, lang.rpad(12, ' '), (like ? 'x' : ' '));
out += ((index+1)%20) ? '' : '\n';
}
return out;
},
print : function(){
me.format().info();
return me;
}
}
$.Edu.YouTube.Channel = me;
})(CKi);
Checking Library
(function(Channel){
Channel.add('Babycare').add('Beverage').add('Bodycare').add('Breeding').add('Carecure');
Channel.add('Interior').add('Inventor').add('Kingdoms').add('Kitchens').add('Literary');
Channel.add('Cultural').add('Diligent').add('Donation').add('Economia', 'Dutch').add('Emporium');
Channel.add('Gardenia').add('Hatchery').add('Healthya', 'Romanian').add('Heritage', 0, 1).add('Historic');
Channel.add('Exterior').add('Fabricat', 'Romanian').add('Fabulous').add('Farmacia').add('Festival');
Channel.add('Academia', 0, 1).add('Agronomy', 0, 1).add('AirCargo').add('Airlines').add('Antiques');
Channel.add('Aquarium', 0, 1).add('Artifact').add('Ascriber').add('Assembla', 'Catalan').add('Atlantic');
Channel.add('Composer').add('Composts', 0, 1).add('Crafting', 0, 1).add('Cruising').add('Cuisines', 0, 1);
Channel.add('Homecare').add('HutBazar', 'Bangla').add('Hygienia', 'Finish').add('Imaginer').add('Imperial');
Channel.print();
})(CKi.Edu.YouTube.Channel);