javascript class.method1.method2 [duplicate] - javascript

This question already has answers here:
How does basic object/function chaining work in javascript?
(5 answers)
Chaining methods with javascript
(3 answers)
Closed 1 year ago.
i'm looking for something like :
document.getElementById("id").innerText.toString().length;
so : class.method1().method2().method3();
my code :
class Example
{
method1(){ .. }
method2(){ .. }
method3(){ .. }
}
from out class :
var element = new Example();
element.method1(); //works fine
element.method1().method2(); // error: method2 is not defined
any solution?
thanks for all :)

What you're looking for is known as a fluent interface. To implement this, just add return this; to the end of every method in the class.
Side note: document.getElementById("id").innerText.toString().length; is not a fluent interface - each function/attr returns a different value.

Related

Method to call a function in javascript in a particular way [duplicate]

This question already has answers here:
How do I write an extension method in JavaScript?
(2 answers)
Add method to string class
(6 answers)
Closed 2 years ago.
I am kinda new to js and would appreciate some help to clarify one subject.
Basically i want to call some functions that i write like default javascript are called:
//declaring function
const splitAsExample = text => text.split('|')
//calling function
splitAsExample('Yesterday|Today|Tomorrow')
Instead of calling the function as mentioned above, i would like to know if it's possible to make a function that can be called like:
'Yesterday|Today|Tomorrow'.splitAsExample()
//and || or
'Yesterday|Today|Tomorrow'.splitAsExample
I learned js all by myself and didn't manage to find a specific name for this question to search up in google. :)
If you can clarify this topic for me it would be great, but if you could give me the name to search it up would be even better!
You could add a prototype function to String.
This allows method chaining with a given object.
String.prototype.splitAsExample = function () { return this.split('|'); };
console.log('Yesterday|Today|Tomorrow'.splitAsExample());

How to call JavaScript class's method from another method? [duplicate]

This question already has answers here:
'this' keyword overriden in JavaScript class when handling jQuery events
(2 answers)
es6 classes and "this" with event handlers [duplicate]
(2 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 5 years ago.
I am using iOS's JavaScriptCore framework but I am having difficulty getting a JavaScript (es6) class to call another method. Here is my playground code.
do {
let j = JSContext()!
j.evaluateScript("class Base123 { method1(arg1) { return this.method2(arg1) } method2(arg1) { return {\"asdf\":\"123\"} } }")
j.evaluateScript("var b = new Base123(); var handler = b.method1")
j.evaluateScript("handler(4)")?.toDictionary() //<-- expected ["asdf": "123"]
}
Essentially I am Creating a class called Base123 with two methods. method1 calls method2. However the call to method2 is undefined.
Does anyone know how to do this?
Reformatting the class definition to make it easier to read:
class Base123 {
method1(arg1) {
return this.method2(arg1)
}
method2(arg1) {
return {"asdf":"123"}
}
}
Is there something wrong with this javascript definition?
Try replacing var handler = b.method1 with var handler = b.method1.bind(b).

How to have a class which is a property of something? [duplicate]

This question already has answers here:
How to namespace es6 classes (for React components)
(3 answers)
Closed 6 years ago.
I have a javascript object menu, and I want to add a property controller which should be a constructor. However, the following gives a syntax error:
class menu.foobar {
// stuff here
}
What is the right way to do this?
Use a class expression:
menu.foobar = class {}

What (jQuery) means in the end of function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 7 years ago.
I have a function:
var waitingDialog = (function($){
.....
return{
}
})(jQuery);
Also could you explain what $ means in the function? Is it going to work without that?
It means that jQuery (if it exists) will be passed to the function. $ is merely the name that variable will take in the scope of the function.
There is a section about that in official jQuery website : https://learn.jquery.com/plugins/basic-plugin-creation/#protecting-the-alias-and-adding-scope

Possible to override a base classes privileged function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an equivalent of PHP's 'parent' with javascript prototypal inheritance?
Override a base class function
I have a base class/prototype & a child class/prototype in Javascript.
Is it possible to make the child class override a base classes privileged function?
I know I am trying to treat javascript as an OO language which its not but you never know this maybe possible?
If its possible could you give an example how I do this?
function baseClass()
{
this.privFunct() {}
}
function childClass()
{
this.privFunct()
{
var baseFunct = baseClass.prototype.privFunct;
this.baseFunct();
// no do some object specific actions here
}
}

Categories

Resources