Objects?

You can bulid an obj in snap,but its hard.Maybe use'call((JS function(return {})))'

Hi, welcome to the Snap! forum!

A Snap! object is not the same as a JS object. To get any benefit from the latter, you'd have to use JS Function to add methods or data, to send messages, to retrieve data, and so on. Essentially at that point you might as well write the program in JS.

Snap! has two kinds of objects. One is sprites (and, lately, costumes and sounds). Those are easy to make, and easy to use, although it takes a little bit of thought to understand inheritance from a prototype sprite. The other is, I agree, harder; you make them yourself out of anonymous procedures. But the difficulty pays off in a much deeper understanding of how programming languages work.

<blocks app="Snap! 5.1, http://snap.berkeley.edu" version="1"><block-definition s="%&apos;key&apos; of %&apos;object&apos;" type="reporter" category="other"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%obj"></input></inputs><script><block s="doIf"><block s="reportEquals"><block var="object"/><block s="reportNewList"><list></list></block></block><script><custom-block s="error %txt"><l>key not found in object</l></custom-block></script></block><block s="doIf"><block s="reportEquals"><block var="key"/><block s="reportListItem"><l>1</l><block s="reportListItem"><l>1</l><block var="object"/></block></block></block><script><block s="doReport"><block s="reportListItem"><l>2</l><block s="reportListItem"><l>1</l><block var="object"/></block></block></block></script></block><block s="doReport"><custom-block s="%s of %obj"><block var="key"/><block s="reportCDR"><block var="object"/></block></custom-block></block></script></block-definition><block-definition s="set %&apos;key&apos; of %&apos;object&apos; to %&apos;value&apos;" type="command" category="other"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%obj"></input><input type="%s"></input></inputs><script><block s="doIf"><block s="reportEquals"><block var="object"/><block s="reportNewList"><list></list></block></block><script><custom-block s="error %txt"><l>Key not found in object</l></custom-block></script></block><block s="doIf"><block s="reportEquals"><block var="key"/><block s="reportListItem"><l>1</l><block s="reportListItem"><l>1</l><block var="object"/></block></block></block><script><block s="doReplaceInList"><l>2</l><block s="reportListItem"><l>1</l><block var="object"/></block><block var="value"/></block><block s="doStopThis"><l><option>this block</option></l></block></script></block><custom-block s="set %s of %obj to %s"><block var="key"/><block s="reportCDR"><block var="object"/></block><block var="value"/></custom-block></script></block-definition><block-definition s="error %&apos;msg&apos;" type="command" category="control"><header></header><code></code><translations>pt:lança o erro _&#xD;</translations><inputs><input type="%txt"></input></inputs><script><block s="doRun"><block s="reportJSFunction"><list><l>msg</l></list><l>throw new Error(msg);</l></block><list><block var="msg"/></list></block></script></block-definition><block-definition s="run method %&apos;key&apos; of %&apos;object&apos; with inputs %&apos;inputs&apos;" type="command" category="other"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%obj"></input><input type="%mult%s"></input></inputs><script><block s="doRun"><custom-block s="%s of %obj"><block var="key"/><block var="object"/></custom-block><block s="reportCONS"><block var="object"/><block var="inputs"/></block></block></script></block-definition><block-definition s="call method %&apos;key&apos; of %&apos;object&apos; with inputs %&apos;inputs&apos;" type="reporter" category="other"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%obj"></input><input type="%mult%s"></input></inputs><script><block s="doReport"><block s="evaluate"><custom-block s="%s of %obj"><block var="key"/><block var="object"/></custom-block><block s="reportCONS"><block var="object"/><block var="inputs"/></block></block></block></script></block-definition><block-definition s="cat %&apos;name&apos;" type="reporter" category="other"><header></header><code></code><translations></translations><inputs><input type="%txt">Bob</input></inputs><script><block s="doReport"><block s="reportNewList"><list><block s="reportNewList"><list><l>&lt;CLASSNAME&gt;</l><l>cat</l></list></block><block s="reportNewList"><list><l>name</l><block var="name"/></list></block><block s="reportNewList"><list><l>alive</l><block s="reportBoolean"><l><bool>true</bool></l></block></list></block><block s="reportNewList"><list><l>meow</l><block s="reifyScript"><script><block s="doIf"><custom-block s="%s of %obj"><l>alive</l><block var="self"/></custom-block><script><block s="doSayFor"><block s="reportJoinWords"><list><custom-block s="%s of %obj"><l>name</l><block var="self"/></custom-block><l>: Meow!</l></list></block><l>2</l></block></script></block></script><list><l>self</l></list></block></list></block><block s="reportNewList"><list><l>sleep</l><block s="reifyScript"><script><block s="doIf"><custom-block s="%s of %obj"><l>alive</l><block var="self"/></custom-block><script><block s="doSayFor"><block s="reportJoinWords"><list><custom-block s="%s of %obj"><l>name</l><block var="self"/></custom-block><l> slept for </l><block var="hours"/><l> hours</l></list></block><l>2</l></block></script></block></script><list><l>self</l><l>hours</l></list></block></list></block><block s="reportNewList"><list><l>die</l><block s="reifyScript"><script><block s="doIf"><custom-block s="%s of %obj"><l>alive</l><block var="self"/></custom-block><script><custom-block s="set %s of %obj to %s"><l>alive</l><block var="self"/><block s="reportBoolean"><l><bool>false</bool></l></block></custom-block></script></block></script><list><l>self</l></list></block></list></block></list></block></block></script></block-definition><block-definition s="new empty %&apos;class&apos;" type="reporter" category="other"><header></header><code></code><translations></translations><inputs><input type="%s">cat</input></inputs><script><block s="doReport"><block s="reportNewList"><list><block s="reportNewList"><list><l>&lt;CLASSNAME&gt;</l><block var="class"/></list></block></list></block></block></script></block-definition><block-definition s="add %&apos;key&apos; = %&apos;value&apos; to %&apos;object&apos;" type="command" category="other"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%s"></input><input type="%obj"></input></inputs><script><block s="doAddToList"><block s="reportNewList"><list><block var="key"/><block var="value"/></list></block><block var="object"/></block></script></block-definition></blocks>

my module that has a bug

Ok,so I cant be lazy and use JS objs in Snap?

This module uses pure JS but dosent quite work...

Now im trying to make snap objs(second,harder one).Finished but erroring! My project which keeps erroring
Not erroring any more.

I have a erroring projectMy project which keeps erroring

Yes,thanks.obj
I just wrote the same code in the manual(and,as you see,bad copying)and I found a bug in it. """
if(<(parent) is a reporter>){
return(call(parent)with inputs(message))
}"""is wrong,it should be"""
if(<(parent) is a command>){
return(call(parent)with inputs(message))
}
"""
Objects are functions with locals and can be callen at any time.They are commands,NOT reporters!
Please fix that in the manual!

I found two transcription errors in your project:


The "10" input to FOR is wrong.

One of the "ITEM 1 OF" blocks is wrong.

Is the code making sense to you? Do you see why it should work?

Which page in the manual ?

Edit:
I've found it page 71
And I confirm. "a" is a ring and I think that as a ring around commands, it is interpreted as a command block.

I think you are misunderstanding commands vs. reporters. A reporter is any block that reports a value. If it has REPORT in it, it's a reporter! In particular CLONE OF reports the newly created object, so it's a reporter.

Rings can surround command scripts or expressions. In the latter case, the ringed expression is a reporter.

but it dosent work if i choose reporter,it only works if i choose command.
Maybe the manual is wrong or this 'is' block has a bug.

I'm not understanding. Could you post your project again?

obj

See other thread; it's the IS_A_ block that's wrong. Thanks.

Logically, as you said before, a ring around a command should return "Reporter" but this not the case.
I've just tested it.
This block
OOP-Demo script pic (1)
returns true.
And this one,
OOP-Demo script pic
returns false

Yes(What language are you using?)(

)

Maybe that block is checking the block type,not the object type.If that's right,then it has a bug.But that is wrong.(sprite,costume are object types)
If that block is checking the block type,please make a block that checks the object type.

French
In the manual, the "object" is custom created "procedurally". A kind of emulation of "Object-Oriented Programming" and not using Snap objects.
The logic of the program in the manual is that the "simili-object" starts as a list, so it doesn't respond to "messages". But, when it is cloned, it is "reported" as a chunk of block commands in a ring, and no more a list. To test this case, we need checking if it is a reporter block or not.
And, as you discovered it, the test is false when we check against reporter block, but true when against command block.