Prototype chain: object function does not have access to prototype property but its instance does - prototypejs

I am reading the MDN docs about prototype chain, and here is an overall example of how prototype works:
function doSomething() {}
doSomething.prototype.foo = 'bar';
const doSomeInstancing = new doSomething();
doSomeInstancing.prop = 'some value';
and the console.log of the following values are:
doSomeInstancing.prop: some value
doSomeInstancing.foo: bar //my question
doSomething.prop: undefined
doSomething.foo: undefined //my quesstion
doSomething.prototype.prop: undefined
doSomething.prototype.foo: bar
So it seems like that the instance of doSomething (doSomeInstancing) has access to the foo property in the prototype of doSomething, but the object function doSomething does not. Does this mean that only instance of the function objects, and not the function objects themselves can have access to the function objects' prototype's properties?

Related

cy.origin throws error when trying to call a function which has the args's variable as a parameter

cy.visit('somesite1.com');
....
....
const args = {
testObject: myObject
};
cy.origin( 'somesite2.com', { args }, ({testObject}) => {
console.log('print out my test obj', testObject);
someFunction(testObject);}
`
I am trying to use the cross domain feature. I have Cypress 10.3.1. I am able to visit the cross domain but I have trouble passing the variable for further testing. The error I see is
ReferenceError
_actions4 is not defined
Variables must either be defined within the cy.origin() command or passed in using the args option.
myObject is a json object.
Can someone help please?
Krithika
The problem is someFunction is defined outside the cy.origin(), and cannot be accessed.
function someFunction() {
...
}
const args = {
testObject: myObject
};
cy.origin( 'somesite2.com', { args }, ({testObject}) => {
someFunction(testObject); // cannot be accessed here
}
There are severe limitations on using executable code inside the origin callback.
See Arguments
The args object is the only mechanism via which data may be injected into the callback, the callback is not a closure and does not retain access to the JavaScript context in which it was declared. Values passed into args must be serializable.
not a closure means you can't reference things outside the origin in the same way a simple function can.
must be serializable means you cannot pass functions into the origin.
Essentially, it seems only cy. commands can be be used inside the origin.

Node Promise with Local Variable

async function A(){
const x = await B(); // reads a file or such thing
return x; // returns a promise
}
var foo;
function setFoo(){
foo = A(); // foo will be set to the promise returned by A()
// but I really want foo to be set to the data
// returned by the promise
}
function C(){
// here I need to use the value of the data returned
// by the promise. There are dozens of functions that
// will use foo. Those dozens of functions are called
// from outside of this module
}
Question - Is there some way to extract the data from the promise so I can use it in a local variable? Or do I need to use promise semantics i.e .then(function(...){...}) in every function like function C that needs to use the local variable foo? Or something else?
Don't use async in a function that does nothing other than return await. Just return the promise and you're done.
function A() {
return B(); // reads a file or such thing, returns a promise
}
Never try to set global/outer-scope variables to capture asynchronous values. Keep returning the promises, chain in worker functions with .then(). Keep returning the promise chain.
function foo(){
return A().then(C);
}
The worker functions (an event handler, really) receive the data:
function C(data) {
// here you get the data returned by B()
}
You could even wait for C() now:
foo().then(function (x) {
// `C()` ans finished and `x` will be whatever `C()` returned.
})

Mocking a class instantiated within the target class

I'm trying to write a Jasmine test for a function of class which instantiates and observes an object from another class. Since I want to keep this test contained to the first class and to simulate different scenarios of the second I want to mock the second. Some pseudo code might help
export class Foo {
startProcessing() {
const bar = new Bar();
const sub = bar.tickTock.subscribe(
state => {
// something went right
},
error => {
// something went wrong
}
);
}
}
I've tried declaring the mock class in my test file, and providing it through TestBed.configureTestingModule's providers attribute, but it always uses the original Bar.
How can I write a test that provides a mock class in place of Bar that I can control?
One way to get around this issue is to pass in object of type Bar through function parameter (dependency injection). That way you can have control over Bar object.

Fallback callback when calling unavailable function

Is it possible to set a fallback callback which is called when the user wants to call a function which does not exists? E.g.
my_object.ThisFunctionDoesNotExists(2, 4);
Now I want that a function is getting called where the first parameter is the name and a stack (or something like that) with the arguments passed. To clarify, the fallback callback should be a C++ function.
Assuming your question is about embedded V8 engine which is inferred from tags, you can use harmony Proxies feature:
var A = Proxy.create({
get: function (proxy, name) {
return function (param) {
console.log(name, param);
}
}
});
A.hello('world'); // hello world
Use --harmony_proxies param to enable this feature. From C++ code:
static const char v8_flags[] = "--harmony_proxies";
v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
Other way:
There is a method on v8::ObjectTemplate called SetNamedPropertyHandler so you can intercept property access. For example:
void GetterCallback(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info)
{
// This will be called on property read
// You can return function here to call it
}
...
object_template->SetNamedPropertyHandler(GetterCallback);

In MATLAB, can a class method act as a uicontrol callback without being public?

In MATLAB 2008a, is there a way to allow a class method to act as a uicontrol callback function without having to make the method public? Conceptually, the method should not be public because it should never be called by a user of the class. It should only be called as a result of a UI event triggering a callback. However, if I set the method's access to private or protected, the callback doesn't work. My class is derived from hgsetget and is defined using the 2008a classdef syntax.
The uicontrol code looks something like:
methods (Access = public)
function this = MyClass(args)
this.someClassProperty = uicontrol(property1, value1, ... , 'Callback', ...
{#(src, event)myCallbackMethod(this, src, event)});
% the rest of the class constructor code
end
end
The callback code looks like:
methods (Access = private) % This doesn't work because it's private
% It works just fine if I make it public instead, but that's wrong conceptually.
function myCallbackMethod(this, src, event)
% do something
end
end
Storing the function handle of the callback as a private property seems to workaround the problem. Try this:
classdef MyClass
properties
handle;
end
properties (Access=private)
callback;
end
methods
function this = MyClass(args)
this.callback = #myCallbackMethod;
this.handle = uicontrol('Callback', ...
{#(src, event)myCallbackMethod(this, src, event)});
end
end
methods (Access = private)
function myCallbackMethod(this, src, event)
disp('Hello world!');
end
end
end

Resources