What does it mean to extend a prototype with a new function in JavaScript? - javascript-objects

I have created a constructor function:
function Application(fullName,age,contactInfo){
this.fullName= fullName;
this.age= age;
this.contactInfo= contactInfo;
}
I am trying to define a new function newFunction and to have it "extend" the Application prototype. newFunction should return the age + 1 for the Application it was called on.
What does "extend" mean? Is it a function? Do I call the objects as an array on my newFunction?

When it comes to constructors and you hear "extend" you can think of "make". But generally we don't say "make" because it sounds like you have to create something from scratch. When you have something existing (the constructor) and you are asked to add something to it like newFunction, "extend" is a better word for it.
So likely you are asked to make a new prototype function for Application. For example after your constructor code example you posted, if you are asked to
extend the Application prototype with a function sayMyName
takes no parameters
that always produces an alert of My name is:, followed by fullName, you could do:
Application.prototype.sayMyName = function() {
alert("My name is: " + this.name);
};
Any objects instantiated using Application constructor, you can now call a .sayMyName(); method to produce the My name is... alert .
So with this as an example, hopefully you now know what to do for your situation.

Related

Calling Apex method with multiple signatures from LWC

I've noticed some interesting behavior in an LWC that I am building and haven't been able to find much info on the cause. Basically I have an Apex method declared with multiple signatures:
// myMethod with 2 param signature
#AuraEnabled
public static void myMethod(String param1, String param2) {
// I expect this method to be invoked when called from the LWC code shown below
....
}
// myMethod with 3 param signature
#AuraEnabled
public static void myMethod(String param1, String param2, Boolean param3) {
// However this method gets invoked instead
....
}
When I attempt to call myMethod from an LWC, and only pass two parameters into the method, I would expect that the 2 param signature method would be invoked, however what happens is that the 3 param signature method is invoked, and the value of null is passed as the third params value.
runJob({param1, param2}).then(value => ... )
Is this expected behavior? When I call the methods through apex, the correct method is invoked by its signature, however this doesn't seem to be the case when calling the method from an LWC.
Is there a way for me to invoke the myMethod Apex method of the correct signature from an LWC?
Edit:
it will be banned at compile time in Summer'22 release. https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_ValidationForAuraEnabledAnnotation.htm&type=5&release=238
Original:
It's worse than you think. runJob({param2, param1}) will work correctly too. "Correctly" meaning their names matter, not the position!
Your stuff is always passed as an object, not a list of parameters. You could have a helper Apex wrapper class and (assuming all fields are #AuraEnabled) it'll map them correctly. If you have list of parameters - well, a kind of unboxing and type matching happens even before your code is called. If you passed "abc" to a Date variable - a JSON deserialization error is thrown even before your code runs, you have no means to catch it.
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_wire_method
If the Apex method is overloaded, the choice of what method to call is
non-deterministic (effectively random), and the parameters passed may
cause errors now or in the future. Don't overload #AuraEnabled Apex
methods.
So... pick different names? Or funnel them so the 3-param version looks at the last param and calls 2-param one if needed and the business logic allows it. Generally - keep it simple, leave some comments and good unit tests or 2 months from now poor maintenance guy will struggle.
And (if you use the Apex PMD plugin and/or sfdx scanner) functions with long parameter lists are frowned upon anyway: https://pmd.github.io/latest/pmd_rules_apex_design.html#excessiveparameterlist
See also
https://github.com/trailheadapps/lwc-recipes/issues/196
https://salesforce.stackexchange.com/questions/359728/apex-method-overloading-is-not-working-when-called-from-lwc-javascript-controlle

AngularJS method invocation with parameters

I'm working on someone else's code and I don't fully understand it yet. Besides, I'm fairly new to AngularJS.
They have a class that looks like this:
return vm.item[vm.function](request).$promise.then(function (data) {
...
});
When my vm.function has no parameters, it works perfectly. I just do
vm.function = "myFunction"
and myFunction gets called as expected.
However, now I need to call a function that has an "id" parameter. I have already tried
vm.function = "myFunction(0)"
vm.function = "myfunction({id:0})"
And even adding an "id" element to the "request" var, but no result so far.
How can I make this work?
vm.function is simply the name of the function, it says nothing of the parameters. Let's break down what's going on here.
vm.function = 'myFunction';
vm.item[vm.function](request)
translates to
vm.item['myFunction'](request)
which is equivalent to
vm.item.myFunction(request)
As you can see, it's already passing a request parameter. To add another, you'd simply do:
vm.item[vm.function](request, id)
Keep in mind when using this pattern that your dynamic function signatures must be compatible. If they begin to diverge (as you say, you're editing someone else's code) you should refactor to handle this in a different way.

CakePHP: How one Behavior use the Behavior function of a Plugin?

I have two scenarios.
Scenario 1:
A ProcessableBehavior written in my APP/Model/Behavior needs to use a function inside another Behavior called Queryable in a Plugin.
How do I call Queryable.Queryable function doSomething from inside ProcessableBehavior?
Scenario 2:
If I write a Plugin Processable that now contains ProcessableBehavior and so this Behavior depends on Queryable.Queryable function doSomething, how do I do the calling?
Inside behaviors you can always invoke model methods. Since behaviors attached behave like those, you should be able to call them as they were part of the model.
// behavior 1
public function myFirstBehaviorMethod(Model $Model) {
// do sth
}
And
// behavior 2
public function mySecondBehaviorMethod(Model $Model) {
$Model->myFirstBehaviorMethod($foo, $bar);
}
The basic idea is that they don't necessarily have to know about it other. You just assume that they are part of the model (as behaviors enrich the functionality of models).
Note that you don't have to pass the $Model object, as it will internally use $Model.
Make sure you attach/load them in the right order.
If one depends on the other you could check on it in setup() etc:
// throw exception if not available
if (!$Model->Behaviors->attached('Queryable') {}

extjs function declaration syntax

In extjs, we often have syntax like this:
someFunction = function(){}
or:
someFunction : function(){}
What is the difference between the two? Also, what enables exts to use this syntax as opposed to the normal javascript syntax?
So far as i know, javascript syntax is like this:
function(){}//No '=' or ':'
There is not ExtJS function syntax. All these methods of defining a function are part of JavaScript and there is nothing new introduced by ExtJS. Lets take each case
function functionname() -
This is most common and is coming from the procedural programming school. Basically you are writing global functions and these are called by other functions in your script
Enter OOP in Javascript.. there is where the next two methods come in! Javascript is very flexible and extensible. Functions can be stored in variables, passed into other
functions as arguments, passed out of functions as return values, and constructed at run-time. You can also have anonymous functions! coming back...
someFunction = function() - In this case, you are storing a function in the variable 'comeFunction'.This variable can be part of an object or separate (But internally everything in javascript is object except for primitive data types).
someFunction : function() - In this case also, you are storing the function in the variable but this is during object declaration. You will see them used in ExtJS because it follows OOP.
You could also inject a method or modify the method you already specified by the above two methods. I hope this helps you understand more about functions.

Ruby C Extension using Singleton

I only wanted to allow one instance of my C extension class to be made, so I wanted to include the singleton module.
void Init_mousetest() {
VALUE mouseclass = rb_define_class("MyMouse",rb_cObject);
rb_require("singleton");
VALUE singletonmodule = rb_const_get(rb_cObject,rb_intern("Singleton"));
rb_include_module(mouseclass,singletonmodule);
rb_funcall(singletonmodule,rb_intern("included"),1,mouseclass);
### ^ Why do I need this line here?
rb_define_method(mouseclass,"run",method_run,0);
rb_define_method(mouseclass,"spawn",method_spawn,0);
rb_define_method(mouseclass,"stop",method_stop,0);
}
As I understand it, what that line does is the same as Singleton.included(MyMouse), but if I try to invoke that, I get
irb(main):006:0> Singleton.included(MyMouse)
NoMethodError: private method `included' called for Singleton:Module
from (irb):6
from C:/Ruby19/bin/irb:12:in `<main>'
Why does rb_include_module behave differently than I would expect it to? Also any tangential discussions/explanations or related articles are appreciated. Ruby beginner here.
Also it seems like I could have just kept my extension as simple as possible and just hack some kind of interface later on to ensure I only allow one instance. Or just put my mouse related methods into a module... Any of that make sense?
according to http://www.groupsrv.com/computers/about105620.html the rb_include_module() is actually just Module#append_features.
Apparently Module#include calls Module#append_features and Module#included. So in our C code we must also call included. Since clearly something important happens there.

Resources