Help with object prototype

I am good at javascript and have some problems creating an onject through a prototype.
I have it:

<script type="text/javascript"> function myclass(a, b, c) { if (arguments.length) { this.Init(a, b, c); } } myclass.prototype.Init = function(a, b, c) { this.param1 = a; this.param2 = b; this.param3 = c; }; myclass.prototype.Print = function() { alert(this.param1 + '-' + this.param2 + '-' + this.param3); }; var myObject = myclass(3, 5, 6); myObject.Print(); </script> 

but I get an error with this .Init (a, b, c);
Error: The object does not support this property or method.

+4
source share
2 answers

You forgot the new keyword when declaring myObject :

 var myObject = new myclass(3, 5, 6); 
+3
source

Just out of curiosity, is there a special reason why you have a separate init method?

The function that defines your “class” is called the “constructor”, and you can just do the customization there. If you want to “reinitialize” an object, then this may be useful, but it doesn't seem to serve here.

For instance:

 // You might as well start wrapping your code now: var myExample = (function myExample () { // A common convention is to start the name of constructors with a // capital letter, one reason is it help makes it more obvious // when you forget the new keyword...Whether you use it or not // is up to you. Also note, calling it "MyClass" is a little // misleading because it not a "class" really. You might // confuse yourself if you think of it as a class too much. // If you're wondering why I put the name twice, it because // otherwise it would be an anonymous function which can be // annoying when debugging. You can just use var MyClass = function () {} // if you want var MyClass = function MyClass(a, b, c) { // This will set each parameter to whatever was provided // or if nothing is provided: null. If you leave out // the || "" part then any // time a value is not provided the parameter will // return "undefined". This may be what you want in some cases. this.param1 = a || ""; this.param2 = b || ""; this.param3 = c || ""; }; // likewise it convention to start most variables/functions lowercase // I think it easier to type/looks better, but do as you please. MyClass.prototype.print = function print() { alert(this.param1 + '-' + this.param2 + '-' + this.param3); }; var myObject = new MyClass(); myObject.print(); }()); 

"Wrapper"

 (function () { //your code here }()); 

This is basically pointless, but in the end you will have to start doing, so now you can start. This is one way to "wrap", there are others.

Basically, your script file was written, if the user ran another script that had the MyClass function, he could overwrite yours or vice versa, causing problems.

"wrapping" stores all this in this function. If you need to do something accessible to external things, you can expose it.

for the comment:

You can access functions and variables from within the wrapper by exposing them to the outside:

 var myApp = (function myApp(){ // The constructor for our "class", this will be available from outside because // we will expose it later var myClass = function(){ //code to set up "class" etc // See how we can use private function within myApp privateFunction(); }; // Here we set up the private function, it will not be available outside myApp // because will will not expose it var privateFunction = function(){ }; // Another public function that we will expose later var otherPublic = function(){}; //now we expose the stuff we want public by returning an object containing // whatever it is we want public, in this case it just myClass and otherPublic return { myClass: myClass, otherPublic: otherPublic }; }()); 

Please note that in this example we just expose the constructor, if you need an instance of an object you will have to collect them in a variable and expose this variable as:

 var theInstance = new myClass(); return { theInstance : theInstance }; 

Now it will be available outside myApp as myApp.theInstance

You can also use a more general packaging scheme:

 var myApp = { myClass: function(){ //if we want to call another function in myApp we have to do it like so: myApp.publicFunction(); }, publicFunction: function(){}, someString: "this is a string" }; 

Here myApp is just an object literal containing your functions, etc. The main difference is that EVERYTHING in myApp can be accessed externally via myApp.name or myApp [name];

0
source

Source: https://habr.com/ru/post/1305934/


All Articles