Skip to main content
Code Review

Return to Answer

deleted 281 characters in body
Source Link
Paul
  • 111
  • 3

If you want you constructor function to be a singleton, you are going to have to make a few tweaks to your code:

var Mui = (function (window, document, undefined) {
 var Mui = function () {
 //the cached instance
 var instance;

 //rewrite the constructor
 Mui = function Mui() {
 return instance;
 };

 //carry over the prototype properties
 Mui.prototype = this;

 //the instance
 instance = new Mui();

 //reset the constructor pointer
 instance.constructor = Mui;

 //all the functionality
 instance.version = {
 Major : '0',
 Minor : '1',
 Bugfix : '0'
 };
 return instance;
};
Mui.prototype.sayHi = function() {
 alert('hi''Hi');
};
 return Mui;
}(window, this.document));
var mui_1 = new Mui();
var mui_2 = new Mui();
alert(mui_1 === mui_2;mui_2); //truetrue​

You can see this working here: http://jsfiddle.net/34vCH/3/

If you want you constructor function to be a singleton, you are going to have to make a few tweaks to your code:

var Mui = (function (window, document, undefined) {
 var Mui = function () {
 //the cached instance
 var instance;

 //rewrite the constructor
 Mui = function() {
 return instance;
 };

 //carry over the prototype properties
 Mui.prototype = this;

 //the instance
 instance = new Mui();

 //reset the constructor pointer
 instance.constructor = Mui;

 //all the functionality
 instance.version = {
 Major : '0',
 Minor : '1',
 Bugfix : '0'
 };
 };
Mui.prototype.sayHi = function() {
 alert('hi');
};
 return Mui;
}(window, this.document));
var mui_1 = new Mui();
var mui_2 = new Mui();
mui_1 === mui_2; //true

If you want you constructor function to be a singleton, you are going to have to make a few tweaks to your code:

var Mui = function () {
 //the cached instance
 var instance;
 //rewrite the constructor
 Mui = function Mui() {
 return instance;
 };
 //carry over the prototype properties
 Mui.prototype = this;
 //the instance
 instance = new Mui();
 //reset the constructor pointer
 instance.constructor = Mui;
 //all the functionality
 instance.version = {
 Major : '0',
 Minor : '1',
 Bugfix : '0'
 };
 return instance;
};
Mui.prototype.sayHi = function() {
 alert('Hi');
};
var mui_1 = new Mui();
var mui_2 = new Mui();
alert(mui_1 === mui_2); //true​

You can see this working here: http://jsfiddle.net/34vCH/3/

edited body
Source Link
Paul
  • 111
  • 3

If you want you constructor function to be a singleton, you are going to have to make a few tweaks to your code:

var Mui = (function (window, document, undefined) {
 var Mui = function () {
 //the cached instance
 var instance;
 
 //rewrite the constructor
 MaiMui = function() {
 return instance;
 };
 
 //carry over the prototype properties
 MaiMui.prototype = this;
 
 //the instance
 instance = new MaiMui();
 
 //reset the constructor pointer
 instance.constructor = Mai;Mui;
 
 //all the functionality
 instance.version = {
 Major : '0',
 Minor : '1',
 Bugfix : '0'
 };
 };
 Mui.prototype.sayHi = function () {
 alert('hi');
 };
 return Mui;
}(window, this.document));
var mai_1mui_1 = new MaiMui();
var mai_2mui_2 = new MaiMui();
mai_1mui_1 === mai_2;mui_2; //true

If you want you constructor function to be a singleton, you are going to have to make a few tweaks to your code:

var Mui = (function (window, document, undefined) {
 var Mui = function () {
 //the cached instance
 var instance;
 
 //rewrite the constructor
 Mai = function() {
 return instance;
 };
 
 //carry over the prototype properties
 Mai.prototype = this;
 
 //the instance
 instance = new Mai();
 
 //reset the constructor pointer
 instance.constructor = Mai;
 
 //all the functionality
 instance.version = {
 Major : '0',
 Minor : '1',
 Bugfix : '0'
 };
 };
 Mui.prototype.sayHi = function () {
 alert('hi');
 };
 return Mui;
}(window, this.document));
var mai_1 = new Mai();
var mai_2 = new Mai();
mai_1 === mai_2; //true

If you want you constructor function to be a singleton, you are going to have to make a few tweaks to your code:

var Mui = (function (window, document, undefined) {
 var Mui = function () {
 //the cached instance
 var instance;
 
 //rewrite the constructor
 Mui = function() {
 return instance;
 };
 
 //carry over the prototype properties
 Mui.prototype = this;
 
 //the instance
 instance = new Mui();
 
 //reset the constructor pointer
 instance.constructor = Mui;
 
 //all the functionality
 instance.version = {
 Major : '0',
 Minor : '1',
 Bugfix : '0'
 };
 };
 Mui.prototype.sayHi = function () {
 alert('hi');
 };
 return Mui;
}(window, this.document));
var mui_1 = new Mui();
var mui_2 = new Mui();
mui_1 === mui_2; //true
Source Link
Paul
  • 111
  • 3

If you want you constructor function to be a singleton, you are going to have to make a few tweaks to your code:

var Mui = (function (window, document, undefined) {
 var Mui = function () {
 //the cached instance
 var instance;
 
 //rewrite the constructor
 Mai = function() {
 return instance;
 };
 
 //carry over the prototype properties
 Mai.prototype = this;
 
 //the instance
 instance = new Mai();
 
 //reset the constructor pointer
 instance.constructor = Mai;
 
 //all the functionality
 instance.version = {
 Major : '0',
 Minor : '1',
 Bugfix : '0'
 };
 };
 Mui.prototype.sayHi = function () {
 alert('hi');
 };
 return Mui;
}(window, this.document));
var mai_1 = new Mai();
var mai_2 = new Mai();
mai_1 === mai_2; //true
default

AltStyle によって変換されたページ (->オリジナル) /