We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I was trying to create a simple library and ran into problems. This is my html file:
<html>
<script src = 'p5.min.js'></script>
<script src = 'mosaic.js'></script>
<script src = 'sketch.js'></script>
<body>
<script>setMosaic(true)</script>
</body>
</html>
mosaic.js is the library I am creating.
The content of mosaic.js is :
p5.prototype._isMosaic = false;
p5.prototype.setMosaic = function(status){
this._isMosaic = status;
console.log('set worked');
};
If I call setMosaic from inside the as shown in the html file, it gives me a function not defined error. But I can call setMosaic() successfully from inside setup() or draw() of sketch.js. Calling setMosaic from outside the sketch works when I define setMosaic in /src/environment/environment.js and build p5.js again.
Is there a way to call setMosaic from outside the sketch?
Answers
prototype
is synonymous of Java'sstatic
.<script>p5.prototype.setMosaic(true)</script>
<script>p5.prototype.isMosaic = true</script>
Hi,
I understand that I can access isMosaic using full name. But there are other more complex functions that I'd be using and hence I want to be able to call the outside the sketch.
Yes, I've made sure that p5.js is loading first. As I said, the strange thing is that setMosaic works if I'm calling it from INSIDE setup() in the sketch. This leads me to think that it is something to do with the namespace. Since setup() is already bound to p5, it can see setMosaic() as well.
And I did try calling p5.setMosaic() outside the sketch, only to get an error saying that p5 is null/undefined.
"AFAIK, JS' prototype is synonymous of Java's static."
Actually, not, quite the contrary. It is a way to define new fields and methods to an existing object / class (to speak in terms of Java).
Attaching them to the prototype ensures all instances of this class will get these methods, and they can access the internals of the object, via
this
.By default, in P5.js, I suppose calls like setMosaic() is automatically bound to the P5 instance, that's why it works in the sketch. To make them to work outside of the sketch, you have to declare them globally, or attached to your own object (class).
p5.prototype.setMosaic(true);
p5.prototype.isMosaic = true;
static
access, we've gotta use "class_name.prototype.static_property"! :-BOther
static
examples:In Java Mode that'd be:
@GotoLoop @PhiLho
Thank you so much. Using the p5.prototype.setMosaic worked. It was so silly of me to not try calling that considering that I tried every other combination (p5.setMosaic, setMosaic, this.setMosaic) possible. I was under the assumption that once I assign the method to the prototype, I can call it by either this.setMosaic() or p5.setMosaic.
Well, the example at my 1st reply it was already so:
<script>p5.prototype.setMosaic(true)</script>
this.setMosaic()
: Much probably,this
refers to the window global variable when inside the ".html".p5.setMosaic()
: it doesn't exist! Property setMosaic is defined within p5's prototype.this
.static
keyword! :Pstatic
1s! :-\"static
fields!static
fields (instance variables in OOP terms) actually goes into an object!static
fields are equivalent to JS's prototype properties!A silly sample trying to prove that. Dunno whether it's clear or not. :-??
Go to http://p5js.org/reference/#/p5/clear, click at "Edit", paste it and "Run" it from there: :bz