Pages

Friday, 21 February 2014

Class and common function jQuery

var helper = (function () {
            var alertmsg = function () {
                alert("hi alert1");
            };
            return { ALERTMSG: alertmsg };
        })();

 helper.ALERTMSG();

var myclass = function (x, y, z) {
            this.a = x;
            this.b = y;
            this.c = z;
        }        
        $.extend(myclass.prototype, {
            sum: function () {
                alert(this.a + this.b + this.c);
            }
        });

    var obj = new myclass(1, 2, 3);
    obj.sum();

/ properties are directly passed to `create` method
var Person = Class.create({
  initialize: function(name) {
    this.name = name;
  },
  say: function(message) {
    return this.name + ': ' + message;
  }
});
   
// when subclassing, specify the class you want to inherit from
var Pirate = Class.create(Person, {
  // redefine the speak method
  say: function($super, message) {
    return $super(message) + ', yarr!';
  }
});
   
var john = new Pirate('Long John');
john.say('ahoy matey');
// -> "Long John: ahoy matey, yarr!"



var Logger = Class.create({
  initialize: function() {
    // this is the right way to do it:
    this.log = [];
  },
  write: function(message) {
    this.log.push(message);
  }
});

No comments:

Post a Comment