}; var obj2 = new Class();
function Class() {}; Class.prototype.foo = function() {alert("foo");}; function Subclass() {}; Subclass.prototype = new Class(); Subclass.prototype.bar = function() {alert("bar");}; var a = new Class(), b = new Subclass(); a.foo(); // alerts"foo" a.bar(); // throws an error b.foo(); // alerts "foo" b.bar(); // alerts "bar" a instanceof Class; // true a instanceof Subclass; // false b instanceof Class; // true b instanceof Subclass; // true
function Class() { this.constructor.prototype.method = function () {}; } console.log(typeof Class.prototype.method); // "undefined" var a = new Class(); console.log(typeof Class.prototype.method); // "function"
var method = a.method; console.log(a.method === method); // true var b = new Class(); console.log(a.method === method); // false
Class obj; //obj is just a reference(not an object) obj = new Class();// obj refers to the object
var Notifier = new Class({ showMessage: function(message) { }, setElementClass: function(klass) { }.protect() }); var notifier = new Notifier(); notifier.showMessage(); notifier.setElementClass(); > Uncaught Error: The method "setElementClass" cannot be called.
Notifier.Email = new Class({ Extends: Notifier, sendEmail: function(recipient, message) { // can call the protected method from inside the extended class this.setElementClass('someClass'); } }); var emailNotifier = new Notifier.Email(); emailNotifier.sendEmail("a", "b"); emailNotofier.setElementClass("someClass"); > Uncaught Error: The method "setElementClass" cannot be called.
require("mootools"); // globals exported and types shimmed var foo = new Class({}); // etc
for (var i = 0; i < 100000; i++) { var x = new Class(); console.log(x.p1); // ignore p2-p99. we don't need them right now. }
var myclass = new Class({ Implements: [Options], options: { // default options: foo: "foobar" }, initialize: function(options) { this.setOptions(options); alert(this.options.foo); } }); new myclass({foo: "bar"}); // alerts bar new myclass(); // alerts foobar
// Create an instance var c = new Class(); // At some point, initiate loading the value c.setAttr1(); // At some point, look to use the value c.accessAttr1().then(function(inst) { console.log(inst.attr1); }); // Somewhere else, look to use the value c.accessAttr1().then(function(inst) { doSomethingElseWith(inst.attr1); });
var Class = function(){}; Class.prototype = {}; Class.staticMethod = function(){ alert('static method'); }; Class.prototype.instanceMethod = function(){ alert('instance method'); }; var instance = new Class(); Class.staticMethod(); // works Class.instanceMethod(); // doesn't work instance.staticMethod(); // doesn't work instance.instanceMethod(); // works
Class name; try { name = new Class(); } catch (Exception e) { do something }
var obj = new Class(); obj.method(); // 1; var unbound = obj.method; unbound(); // undefined; // Call and Apply setting the context to obj. unbound.apply(obj); // 1 unbound.call(obj); // 1; // ECMAScript 5's bind var bound = unbound.bind(obj); bound(); // 1;
Class name; try { name = new Class(); } catch (Exception exp) { exp.printStackTrace(); }