Skip to content

Instantly share code, notes, and snippets.

@ghelobytes
Created June 17, 2014 08:15
Show Gist options
  • Save ghelobytes/35fd4d90652291fdc1eb to your computer and use it in GitHub Desktop.
Save ghelobytes/35fd4d90652291fdc1eb to your computer and use it in GitHub Desktop.
Sample Javascript
<html>
<head>
<link href="http://cdn.sencha.com/ext/gpl/4.2.0/resources/css/ext-all.css" rel="stylesheet" />
<script src="http://cdn.sencha.com/ext/gpl/4.2.0/ext-all.js"></script>
</head>
<script>
Ext.define('Animal', {
name: 'defaultName',
constructor: function(config) {
Ext.apply(this, config || {});
},
makeNoise: function(){
alert('sssssh!!!');
}
});
Ext.define('Dog', {
extend: 'Animal',
breed: 'defaultBreed',
constructor: function(config) {
Ext.apply(this, config || {});
},
makeNoise: function(){
this.bark();
},
bark: function(){
alert('woof!');
}
});
Ext.define('Cat', {
extend: 'Animal',
constructor: function(config) {
Ext.apply(this, config || {});
},
makeNoise: function(){
if(this.breed == 'pusakal')
alert('ngyaw!');
else
alert('meowww!');
}
});
var dog = Ext.create('Dog', {
name: 'aso',
breed: 'askal',
});
var cat = Ext.create('Cat', {
name: 'pusa',
breed: 'persian',
});
console.log(cat);
cat.makeNoise();
// basic examples
function basicExamples(){
// 0 1 2 3 4
var items = [1,2,3,'A',{x:121,y:14},5,6];
// get last item
console.log(items[items.length-1]);
for(var i in items){
console.log(items[i]);
}
for(var i = 0; i < items.length; i++){
console.log(items[i]);
}
// parsing values
var val = '123.55';
console.log(parseFloat(val)+1);
var animal = {
type: 'dog'
}
console.log(animal);
// change the type
animal.type = 'cat';
console.log(animal);
// change the type again
animal['type'] = 'doggie';
console.log(animal);
animal.makeNoise = function(sound){
alert(sound);
}
animal.makeNoise('woof!');
// test if age is defined
if(animal.age) {
console.log(animal.age);
}
animal.age = 5;
// test if age is defined
if(animal.age) {
console.log('animal = ' + animal.age);
}
delete animal.age;
console.log('animal = ' + animal.age);
// check if age exist, if not, assign 10
if(!animal.age){
animal.age = 10;
}
console.log('animal = ' + animal.age);
delete animal.age;
animal.age = 30;
animal.age = animal.age || 20;
console.log('animal = ' + animal.age);
}
basicExamples();
// asynchronous javascript - non blocking
// synchronous javascript - blocking
// a long process
function longProcess(){
for(var i = 0; i < 1000000; i++){
document.write(i);
}
}
// synchronous
function synchronousExample(){
console.log('start', new Date());
longProcess();
console.log('end', new Date());
}
//synchronousExample();
// another long process
function longProcess2(callback){
for(var i = 0; i < 1000000; i++){
document.write(i);
}
if(callback)
callback();
}
// asynchronous
function asynchronousExample(){
console.log('start', new Date());
longProcess2(function(){
console.log('long process done!');
});
console.log('end', new Date());
}
//asynchronousExample();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment