// Example 1
var someClass = new Class ({
initialize: function () {
alert ( 'Hello, world!' );
}
}); * This source code was highlighted with Source Code Highlighter .
var classObject = new someClass (); * This source code was highlighted with Source Code Highlighter .
// Example 2
var testClass = new Class ({
initialize: function (element) {
this .element = $ (element);
},
draw: function () {
this .element.setStyles ({
'background-color' : 'red' ,
border: 'solid 1px blue'
});
},
hide: function () {
this .element.setStyle ( 'display' , 'none' );
},
show: function () {
this .element.setStyle ( 'display' , 'block' );
},
toggle: function () {
this .element.style.display == 'none' ? this .show (): this .hide ();
}
}); * This source code was highlighted with Source Code Highlighter .
var elMgr = new testClass ( 'el' );
elMgr.draw ();
( function () {
elMgr.toggle ();
}). periodical (1000); * This source code was highlighted with Source Code Highlighter .
// Example 3
var exampleClass3 = new Class ({
Implements: [Options],
options: { // default options
bgColor: 'red' ,
borderWidth: 1
},
initialize: function (element, options) {
this .setOptions (options);
this .element = $ (element);
},
draw: function () {
this .element.setStyles ({
'background-color' : this .options.bgColor,
'border-style' : 'solid' ,
'border-color' : 'black' ,
'border-width' : this .options.borderWidth
});
}
}); * This source code was highlighted with Source Code Highlighter .
var elMgr = new exampleClass3 ( 'el' , {
bgColor: 'yellow'
}); * This source code was highlighted with Source Code Highlighter .
// example 4
someClass = new Class ();
someClass.implement ({
extraMethod: function () {
alert ( 'I am an added method' );
}
});
( new someClass) .extraMethod (); * This source code was highlighted with Source Code Highlighter .
// example 5
var extendedSomeClass = new Class ({
Extends: someClass,
initialize: function () {
this .parent (); // call the base class initialize method
alert ( 'extended Hello, world!' );
}
});
new extendedSomeClass (); * This source code was highlighted with Source Code Highlighter .
// example 6
var exampleClass6 = new Class ({
Implements: [Options, Events],
options: { // default options
bgColor: 'red' ,
borderWidth: 1
},
initialize: function (element, options) {
this .setOptions (options);
this .element = $ (element);
},
draw: function () {
this .element.setStyles ({
'background-color' : this .options.bgColor,
'border-style' : 'solid' ,
'border-color' : 'black' ,
'border-width' : this .options.borderWidth
});
this .fireEvent ( 'draw' ); // event draw
}
});
var obj = new exmpleClass6 ( 'el' , {
bgColor: '# 561' ,
borderWidth: 4
});
obj.addEvent ( 'draw' , function () {
alert ( 'draw completed' );
});
obj.draw (); * This source code was highlighted with Source Code Highlighter .
var obj = new exampleClass6 ( 'el' , {
bgColor: '# 561' ,
borderWidth: 4,
onDraw: function () {
alert ( 'draw completed' );
}
}); * This source code was highlighted with Source Code Highlighter .
// example7
var someClass = new Class ({
Implements: [Options],
options: {
initialize: function () {
alert ( 'after initialize' );
}
},
initialize: function () {
alert ( 'initialize' );
}
}); * This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/39794/