Mimicking Java-style inheritance in Javascript [duplicate] - javascript

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

Related

What is the syntax for private method in browser Javascript class(not node.js)? [duplicate]

This question already has answers here:
Private properties in JavaScript ES6 classes
(41 answers)
Closed 4 years ago.
I know this question has been asked for many times, however, most of them are either for the Node.js solutions or does not work in the browsers.
So, is it possible to implement a private method in browser javascript class?
<html>
<body>
<script>
class Happy
{
constructor()
{
}
_leo()
{
console.log("ff");
}
}
var h=new Happy();
h._leo();
</script>
</body>
</html>
However, the above code still outputs "ff" in the console, how can I fix it?
This illustrates how to hide a method name, effectively making the method uncallable outside the hiding closure:
const Klass = (function() {
const privName = Symbol("priv");
class Klass {
publ() {
console.log("in publ");
this[privName]();
}
[privName]() {
console.log("in priv");
}
}
return Klass;
})()
let inst = new Klass();
inst.publ();
// => in publ
// in priv
It uses the fact that Symbol() returns an entity that cannot be replicated, the fact that privName is not exposed anywhere outside the closure, and the fact that we can define properties with calculated names using the square bracket notation.
Unlike the function-hiding mechanism demonstrated by ultratoad, this is not just a helper function, it is a true method. Just note that you can't call it using the dot notation - dot notation only works when you know the name, and here, no-one does except privName.

Is it bad practice to have arbitrary code and local variable functions defined within a constructor function in JavaScript? [duplicate]

This question already has answers here:
encapsulation in javascript module pattern
(1 answer)
JavaScript Encapsulation
(7 answers)
Preferred way of encapsulating Javascript and defining private variables/methods?
(2 answers)
Closed 5 years ago.
I am tasked with refactoring some code from a former employee. He uses a constructor function that has two things that seem strange – 1, arbitrary code that runs outside of a function or property, and 2, locally defined functions. This is a simplification of what he is doing:
var Dog = function(){
// Arbitrary code
console.log('I am a dog');
var foo = 'foo';
// Function defined to local variable
var bar = function(){
console.log(foo);
console.log('bar');
};
// Normal function in a constructor
this.bark = function(){
console.log('bark');
};
};
var d = new Dog();
Is there any merit to this style of constructor function? Or would it be better to refactor it to only define functions using this in the style of bark, and then running them on d as needed?
Well I don't think there is any special 'merit' is just one in many ways of doing things. The code is simply run on instantiation.
The 'bar' function is hidden from the class user (or 'private' if you like the old OO name), and the 'bark' function can be called from outside the class.
It's one of the ways of doing some sort of OO in JS. There are plenty.
Javascript is a very 'free' language. You see people using different ways of doing things all the time. There is not only one 'right' way. Get used to it :)

Need to know which is the correct way of implement constructor function [duplicate]

This question already has answers here:
Constructor function vs Factory functions
(8 answers)
Closed 6 years ago.
Need to know which is the correct way of implementing the constructor function from these two options.
var Dog=function(name,bread)
{
return {
name:name,
bread:bread
}
}
function Dog(name,bread)
{
var new_object= this;
new_object.name=name;
new_object.bread=bread;
}
The is not one correct way to do that in JavaScript. Refer to this answer to see the different patterns that you can use to program in an object-oriented way in JavaScript:
https://stackoverflow.com/a/30148923/1566187
It depends on what you want to do with it, but if you're looking for a simple constructor I'd recommend
function dog(name,breed) {
this.name = name;
this.breed = breed;
}
This way you can create new objects easily using that constructor:
var Dog = new dog("Jacky", "Corgi");
var Puppy = new dog("T-Rex", "Yorkshire");
Hope this helps. :)

Why we use "prototype" property to add methods to constructor instances? [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 7 years ago.
I am creating a JavaScript constructor. Consider this example:
function Student(name)
{
this.name = name;
}
Student.prototype.printName = function(){
console.log(this.name);
}
var s = new Student("Eden");
s.printName();
This above code works all well. But I can write the same this way also:
function Student(name)
{
this.name = name;
this.printName = function(){
console.log(this.name);
}
}
var s = new Student("Eden");
s.printName();
I feel the second method is correct way because printName should be own property of new object not an inherited property. Adding to prototype makes it an inherited property.
Why do developer prefer the first method?
By attaching functions directly to this in a constructor, that means every instance of Student has it's own copy of printName. That isn't very efficient. By attaching it to the prototype chain of Student, there is only one copy of the printName function and any instance of Student has it in it's prototype chain.
While there are other differences in those two approaches for attaching function to objects, that is the simplest and easiest to remember.
If you want the full dictionary of reasons how the two differ, please refer to this answer to an extremely similar question.

What is the point of prototypes in JavaScript? Why not add method directly to constructor? [duplicate]

This question already has answers here:
Declaring javascript object method in constructor function vs. in prototype [duplicate]
(3 answers)
Closed 9 years ago.
So I am going over a few lessons over JavaScript at CodeAcademy, and I ran into this section regarding prototypes.
A bit from the code:
function Dog(breed){
this.breed = breed;
};
Dog.prototype.bark = function(){
console.log("Bark!");
};
I don't understand the point of doing it that way. Why not just do it this way? (The way I first learned.)
function Dog(breed){
this.breed= breed;
this.bark = function(){
console.log("Bark!");
};
}
The second way, everything is together in one block, and not separated. What is the advantage of the first one? I know that there is, I'm just not seeing it.
One difference is, that in the prototype case the function exists only once and changing it will change all objects using this prototype. In the this. case the function is repeated in every object.
This makes a difference both in footprint and semantics.
it is for performance reasons. when creating a new instance of objects and calling a native method you are eating more memory for every creation.
On the other hand when using prototype it is more memory safe regardless the amount of Object created.

Categories

Resources