I've started to try learn how to use JavaScript Objects more extensively and I've been attempting to write an Object in JavaScript that allows me to set some Constructor arguments on initialisation.
I've got to say, there's quite a few different 'design patterns' in JavaScript that I've probably got myself a little mixed up with syntax and whatnot. Through my research I've found various StackOverflow articles such as:
What I'd like to do with my object set some internal/private variables at the point of initialisation like below:
<script>
var TestObj = new Dispatch( 100 );
console.log( TestObj.getConstructorValue() );
//Would Return 100.
</script>
Although Currently, The way the object is built up is currently Test
returning undefined when attempting to access it after it being initialised:
<script>
$(document).on('ready', function(){
var TestObj = new Dispatch( 100 );
//Set post-initialised variables & set to '5'
TestObj.setOrderNumber( 5 );
//Retrieves 5
console.log( "Accessing Property: " + TestObj.OrderNumber );
//Method for getting orderNumber Property, Returns 5
console.log( "Method for Order Number: " + TestObj.getOrderNumber() );
//Method for getting would-be constructor value
console.log( TestObj.getTest() ); //Returns Undefined
console.log( TestObj.Test ); //Returns Undefined
});
</script>
<script>
/**
*
**/
var Dispatch = function( Arg1 ) {
var OrderNumber;
var Test;
var setOrderNumber = function( orderNum ) {
this.OrderNumber = orderNum;
};
this.constructor = function( str ) {
this.Test = str;
};
this.constructor( Arg1 );
return {
/**
* Getter for OrderNumber
**/
getOrderNumber : function(){
return this.OrderNumber;
},
/**
* Setter for OrderNumber
**/
setOrderNumber : setOrderNumber,
/**
* Getter for Test
**/
getTest : function() {
return this.Test;
}
};
};
</script>
I've attempted to set it directly:
<script>
var Dispatch = function( s ) {
/**
* Assign constructor
* to Test
**/
var Test = s;
return {
getTest : function() {
return this.Test;
}
}
};
TestObj.getTest(); //Returns undefined
</script>
I've also attempted accessing the variable by mixing up the function return slightly:
<script>
var Dispatch = function( s ) {
var Test;
var getTestVar = function() {
return this.Test;
}
this.constructor = function( str ) {
this.Test = str;
};
/**
*
**/
this.constructor( s );
return {
getTest : getTestVar
};
};
TestObj.getTest(); //Returns undefined
</script>
I've toyed around with other methods, although, It would be nice to get an written understanding to why I've gone wrong to making my constructor work.
Here's a jsFiddle that shows all this in action. Apologies for quite a long post & my JavaScript Object ignorance!
You have really confused many concepts.
This is what you are looking for:
var Dispatch = function( s ) {
/**
* Assign constructor
* to Test
**/
this.Test = s;
this.getTest = function() {
return this.Test;
}
};
Thus:
TestObj = new Dispatch(7);
Will result in the object:
Dispatch {Test: 7, getTest: function}
and:
TestObj.getTest();
Will return 7.
You can look here for more proper info about constructors:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor