I'm trying to call a public function inside a class using the immediate window using the following:
?LeadFunctions.Duplicate(2)
Reference to a non-shared member requires an object reference.
The function is defined inside my LeadFunctions class, and is a public function. Why does this not work?
Related
We can access static variables in static method by using 'this' keyword but this statement does not work if we pass that static method in any route. Keyword 'this' has refrence to class but its value is undefined. How?
Actucal code is big, I have given only problematic code if you want you can run it.
Router code :-
import UserController from './user-controller';
router.post('/register', UserController.register);
/////////////////////////////////////////////////////////
Controller Code :-
static someString = 'myString';
static register() {
console.log(this.someString);
}
}
export default UserController;
/////////////////////////////////////////////////////////
Error :-
TypeError: Cannot read properties of undefined (reading 'someString')
at register (/home/user/Desktop/TS-Pro/src/modules/user/user-controller.ts:5:22)
at Layer.handle [as handle_request] (/home/user/Desktop/TS-Pro/node_modules/express/lib/router/layer.js:95:5)
at next (/home/user/Desktop/TS-Pro/node_modules/express/lib/router/route.js:144:13)
at Function.joiValidation (/home/user/Desktop/TS-Pro/src/utils/validator.ts:41:7)
at /home/user/Desktop/TS-Pro/src/modules/user/user-validator.ts:11:22
at Generator.next (<anonymous>)
at /home/user/Desktop/TS-Pro/src/modules/user/user-validator.ts:8:71
at new Promise (<anonymous>)
at __awaiter (/home/user/Desktop/TS-Pro/src/modules/user/user-validator.ts:4:12)
at register (/home/user/Desktop/TS-Pro/src/modules/user/user-validator.ts:18:16)
Some general Object Oriented stuff:
A Class is a blueprint. Example: Vehicle
An Object is a specific item created using that blueprint. Example: car (of type Vehicle)
Keywords like this/me/self are used to refer to an instance of an Object from within the same object. I want to emphasize, that an instance of an object means a specific car object and not the Vehicle class.
Keyword static marks an attribute/method as being Class-level (i.e. you can call it without creating an object). e.g. Vehicle.Accelerate and not car.accelerate.
Keyword static ALSO makes that attribute global. i.e. you can change it in 1 object and it will change for all objects, because it's actually sitting in central blueprint, and not a specific object. (not relevant to your question, but it's the next mistake people make)
The short version: static methods only work with static methods/variables. Instance methods (i.e. methods without static) can access all methods/variables. (Ignoring public/private visibility)
In most high level languages this is inaccessible in static context and your code would cause a syntax error, but JavaScript doesn't many of the limitations because any text can be almost anything (method, class, variable and more) and turn into almost anyting mid-way. It's something you have to keep in mind.
You get an error, because JS compiler doesn't check for undefined variables ahead of time and you are trying to access a different (non-static) variable by using keyword this.
Finally, To access that variable either use that variable name directly (while within the class), or use ClassName.variableName syntax.
The documentation provided on python.org for class Property, states that:
"A property object has getter, setter, and deleter methods usable as decorators that create a copy of the property with the corresponding accessor function set to the decorated function."
What I can't wrap my head around is this: why make a new copy (and hence the overhead of duplication) of the existing Property object, with an extra accessor function set in place, and not just simply augment it (the object) by filling in that accessor function into its corresponding vacant slot (fset / fdel), and then return it, the original object itself?
What all inbuilt function call happen after creating a object from class? I know init() get called any other functions get called automatically ?
I don't think anything occurs after init() unless you define it to, before init(); however a call to the class' __new__() function will occur BEFORE the call to init()
I'd like to use a single duktape/C constructor function as dispatcher for these kind of calls. When the dispatcher function is called I need to know for which class this happend to call the appropriate C++ construction function.
I guess the this binding won't help since it represents the (not yet fully initialized) JS object we are creating here.
Another option would be the current function, but from the docs I can't see how to get the class name from that. What else could I use?
Could you elaborate what you mean by "class name"? Do you mean the .name property of the Ecmascript function object which is used as a the 'new' target?
If so, you can use duk_is_constructor_call() to see if the current call is a constructor call, then use duk_push_current_function() to get access to the Ecmascript constructor function object, and then read its properties using the usual property API calls. For example, if by "class name" you mean .name of the function object, you'd just read its "name" property using duk_get_prop_string().
To load a DLL and call function in VC++, we use LoadLibrary and GetProcAddress , which needs mangled name of the method. Does it instantiate a class object and then call the method?
If it does not then how can I call a method in a class but instantiating an object of the class and then call a method?
What is process to load .h file, load class, instantiate object and then call a method in VC++
There is actually a very good example on CodeProject that describes exactly how to do this.
EDIT with reference to your comment, if you read the above article, you'll see that GetProcAddress() does nothing more than return a function pointer. If you want to create an instance of a class that's exported from the DLL, you have to allocate memory and force the system to invoke the constructor of the class to instantiate it there. Once that's done, however, I'm sort of assuming that you can call functions defined in the class in the normal way by using the object you just created.