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

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
}
}

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());

ES6 class member to be a class itself [duplicate]

This question already has answers here:
Nested ES6 classes?
(4 answers)
Closed 6 years ago.
in es6 a class can have methods and static methods as its members. can it also have classes as members? (because Class is actually kind of function itself?)
i would like it to build nested classes.
class foo {
barMethod {
// do something for the instance
}
static bamMethod {
// do something
}
// e.g.
class batClass {
// the constructor and other stuff for batClass
}
}
new foo.batClass();
[No.] (Nested ES6 classes?). Read this post. You can only do as #floribon mentioned in comment.

Calling static method from constructor es6 [duplicate]

This question already has answers here:
Call static methods from regular ES6 class methods
(3 answers)
Closed 6 years ago.
When I am trying to call a static method from constructor in javascript it says the method doesn't exist.
class TestClass {
constructor(){
this.staticMethod();
}
static staticMethod() {
}
}
This works fine if i try to call a normal method from constructor. If static methods belongs to class instead of instance why it's not allowing them to call from constructor?
this.constructor.staticMethod()
can be used to avoid referring to the class directly (particularly useful for class inheritance and pasted code).
You have to call it like this:
TestClass.staticMethod()

Mimicking Java-style inheritance in Javascript [duplicate]

This question already has answers here:
What is the reason to use the 'new' keyword at Derived.prototype = new Base
(6 answers)
Closed 7 years ago.
I'm trying to mimic Java-style class inheritance and I thought I'd gotten it. However, I'm having an issue that when I create multiple instance of the same Javascript class, all instances are sort of created the same.
Here is the situation.
I have a Javascript class called Screen:
function Screen(screenName) {
this.current = new DataResultSet(); // this is a java class
this.current.setScreenName(screenName);
}
Screen.prototype.GetScreenName= function() {
return this.current.getScreenName();
}
Screen.prototype.GetFieldValue= function(name) {
return this.current.getValue(name);
}
// some other functions that can be called that utilize the current object.
This is the inherited class:
function screenSub1() {}
screenSub1.prototype=new Screen("screenSub1");
screenSub1.prototype.GetID = function() { return GetFieldValue("ID"); }
// some other functions that can be called, specific to this class
Now, in my code, if I do
var obj = new screenSub1();
var obj2 = new screenSub1();
The underlying "current" object for both objects are the same! Is there anyway to get around this issue? Thanks!
Julia
As far as I know, you can't (at least not easily, maybe you could find some javascript library that mimics java-like inheritance). Java and Javascript are very different when it comes to inheritance. You can check here if You want to have further information on the subject : https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

Creating "static" members in a javascript object [duplicate]

This question already has answers here:
Static variables in JavaScript
(43 answers)
Closed 8 years ago.
Is there a way to create "static" members in a JS Object?
function Person(){
}
Person.prototype.age = null;
Person.prototype.gender = null;
I would like to add personsCount as a static member, is that possible?
Sure, just add Person.personsCount without the prototype
Common practise is to make such "static members" properties of the constructor function itself:
function Person() {
Person.count++;
}
Person.count = 0;

Categories

Resources