classCounter{add(amount){// Code I write}}const counter =newCounter();// Call add method.
counter.add(1);
Objective-C classes allow sending messages
// Send add message.[counter add:@1];// Here is the identifier for this message.
SEL messageIdentifier =@selector(add:);// Send add message.[counter performSelector:@selector(add:) withObject:@1];// Create message ready to send.// Yes, it is a fair amount of code…
NSMethodSignature *signature =[Counter
instanceMethodSignatureForSelector:@selector(add:)];
NSInvocation *sender =[NSInvocation
invocationWithMethodSignature:signature];
NSNumber *amount =@1;[sender setArgument:&amount atIndex:2];[sender setTarget:counter];// Now, let’s send the add message twice.[sender invoke];[sender invoke];
Adding sent numbers
// This is our message generator:function*GenerateNumbers(){yield1;// Theseyield2;// areyield3;// our messages}// This is our message processor:functionadder(messageGenerator){let total =0;for(const message ofmessageGenerator()){
total += message;}return total;}adder(GenerateNumbers);// 6