How do I create a class in javascript - javascript

I need a student class in javascript with 2 data members Name and Age and 2 method get_record() and set_record(name,age). How do I do it in javascript and create multiple object of that class.

var Student = function(age, name){
this.age = age;
this.name = name;
this.get_age = function(){
return this.age;
}
this.get_name = function(){
return this.name;
}
this.set_age = function(age){
this.age = age;
}
this.set_name = function(name){
this.name = name;
}
}
var student = new Student(20,"XYZ");

You can model classes with new JavaScript based languages. Dart and TypeScript are probably the most popular in this respect.
This example is based on the JavaScript output from a TypeScript class.
var Student = (function() {
function Student(name, age) {
this.name = name;
this.age = age;
}
Student.prototype.get_record = function() {
return "Name: " + this.name + "\nAge: " + this.age;
}
Student.prototype.set_record = function(name, age) {
this.name = name;
this.age = age;
}
return Student;
})();
// Usage
var a = new Student("John", 23);
var b = new Student("Joe", 12);
var c = new Student("Joan", 44);

function student (age,name) {
this.name = name;
this.age = age;
this.get_record = function() {
return "name:"+this.name+" , age:"+this.age;
}
this.set_record = function(_name,_age) {
this.name=_name;
this.age=_age;
}
}

You can use 'constructor function'.
function Student() {
this.get_record = function(){ return this.name; };
this.set_record = function(name, age) {
this.name = name;
this.age = age;
};
return this;
}
var student1 = new Student();
var student2 = new Student();
student1.set_record('Mike', 30);
student2.set_record('Jane', 30);
student1.get_record();
student2.get_record();
More complex class structures are constructed via prototypes

Related

prototypal inheritance javascript

I am getting below error while using constructor function to implement prototypal inheritance in JavaScript.
I am trying to achieve custom (user defined) prototype chain like this -
function Human() {
this.species = "Homo Sapiens";
}
Human.prototype.run = function () {
console.log("Running...");
};
function Person(gender, age, name) {
Human.call(this);
this.name = name;
this.gender = gender;
this.age = age;
}
Person.prototype.printAge = function() {
console.log(this.age);
}
Person.prototype.printGender = function() {
console.log(this.gender);
}
function Employee(gender, age, name, dept, salary) {
Person.call(this, gender, age, name);
this.department = dept;
this.salary = salary;
}
Person.prototype = Object.create(Human.prototype);
Person.prototype.constructor = Person;
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
const employee = new Employee("female", 28, "Radha", "Manager", 50000);
employee.run();
employee.printAge();
Uncaught TypeError: employee.printAge is not a function
See mdn docs to learn why it's not working
Better use:
Object.setPrototypeOf(
Derived.prototype,
Base.prototype,
);
function Human() {
this.species = "Homo Sapiens";
}
Human.prototype.run = function () {
console.log("Running...");
};
function Person(gender, age, name) {
Human.call(this);
this.empname = name;
this.gender = gender;
this.age = age;
}
Person.prototype.printAge = function() {
console.log(this.age);
}
Person.prototype.printGender = function() {
console.log(this.gender);
}
function Employee(gender, age, name, dept, salary) {
Person.call(this, gender, age, name);
this.department = dept;
this.salary = salary;
}
Object.setPrototypeOf(
Person.prototype,
Human.prototype,
);
Object.setPrototypeOf(
Employee.prototype,
Person.prototype,
);
const employee = new Employee("female", 28, "Radha", "Manager", 50000);
employee.run();
employee.printAge();

the incrementAge function is returning undefined/NaN when invoked after defining the new User

sorry, the incrementAge function is returning undefined/NaN when invoked after defining the new User. I am not sure what's wrong
function User(name,age){
this.name = name;
this.age = age;
}
User.prototype.incrementAge = ()=>{
return this.age++;
}
const mike = new User("Mike",20);
console.log(mike.incrementAge());
The correct way to do this is to create a User class and create a method to raise the value of the variable age.
As you can see by calling the increment age method several times the value is added.
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
incrementAge() {
return ++this.age;
}
}
const mike = new User("Mike", 20);
console.log(mike.incrementAge());
console.log(mike.incrementAge());
The solution (by #ChrisG in comments)
function User(name, age) {
this.name = name;
this.age = age;
}
User.prototype.incrementAge = function () {
return ++this.age;
}
const mike = new User("Mike", 20);
console.log(mike.incrementAge());

Inherit function in javascript

I have a function that is similar to each other. How can I make declaring a function easier without duplicating
function constructor (name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// create a Penguin constructor here
function Penguin(name, numLegs){
this.name=name;
this.numLegs = numLegs;
}
// create a sayName method for Penguins here
Penguin.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// our test code
var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();
You were almost there.
// create a Penguin constructor here
function Penguin(name, numLegs){
Animal.call(this, name, numLegs);
};
// Reuse the prototype chain
Penguin.prototype = Object.create(Animal.prototype);
Penguin.prototype.constructor = Penguin;

Javascript Singleton communication definition required

I made a object to keep my functions became singleton, using that, i made sample methods to call and communicate each other.. but i don't get any appropriate results..
any one correct me, the singleton the way i defined here...
my sample codes:
var obj = window[obj] || {}; //singleton
obj.nameIt = function(name){
this.name = name;
this.getName = function(){
return this.name;
}
}
obj.sayIt = function(name){
this.name = name; var that = this;
this.sayHello = function(){
console.log("say" + this.name);
that.getName();//how to get result from nameIt?
}
}
var x = obj.nameIt("af");
console.log(x.getName());//says "undefined" - how to call it?
var y = obj.sayIt("xy");
console.log(y.sayHello());//says "undefined" - how to call it?
jsfiddle here
Your code does not return anything.
obj.nameIt = function(name){
this.name = name;
this.getName = function(){
return this.name;
}
return this;
}
obj.sayIt = function(name){
this.name = name; var that = this;
this.sayHello = function(){
console.log("say" + this.name);
return that.getName();
}
return this;
}

javascript private variable through inheritance

I'm stuck with a problem using javascript...
I want to declare a private variable in a class that can't be used from its sublclass...what I've tried is:
function Person(){
var _name
this.setName = function(name){
_name = name
}
this.getName = function(){
return _name
}
}
function GreetingPerson(){
var self = this;
self.sayHello = function(){
console.log(self.getName() + ': "Hello!"');
}
}
GreetingPerson.prototype = new Person()
GreetingPerson.prototype.contructor = GreetingPerson;
var manuel = new GreetingPerson()
manuel.setName('Manuel');
manuel.sayHello();
var world = new GreetingPerson()
world.setName('World');
world.sayHello();
manuel.sayHello();
console.log(manuel.name)
In this way the name variable is private, but it is also static, so the last wo sayHello method calls, will write the same output.
I've tried also changing the Person class in this way:
function Person(){
this.setName = function(name){
this.name = name
}
this.getName = function(){
return this.name
}
}
But in this way it is not longer private.
What is the correct way to achieve it?
EDIT: Using something like #teddybeard says, you can get it too:
function Person(){
var _name;
this.setName = function(name){
_name = name;
};
this.getName = function(){
return _name;
};
return this;
}
function GreetingPerson(){
Person.call(this);
this.sayHello = function(){
console.log(this.getName() + ': "Hello!"');
};
return this;
}
GreetingPerson.prototype = new Person();
GreetingPerson.prototype.constructor = GreetingPerson;
var manuel = new GreetingPerson();
manuel.setName('Manuel');
manuel.sayHello();
var world = new GreetingPerson();
world.setName('World');
world.sayHello();
manuel.sayHello();
console.log(manuel._name);
But I'm not pretty sure if this is actually ok. The problem is that if you don't do something like Person.call(this); inside the constructor of GreetingPerson, you will not create a new instance of Person and it will always use the same _name value.
Check out Eloquent Javascript if you have time. I think this code should work for your purposes of inheritance.
function Person() {
var _name
this.setName = function(name) {
_name = name
}
this.getName = function() {
return _name
}
}
function GreetingPerson() {
Person.call(this);
this.sayHello = function() {
console.log(this.getName() + ': "Hello!"');
}
}
// taken from Eloquent Javascript
function clone(object) {
function OneShotConstructor() {}
OneShotConstructor.prototype = object;
return new OneShotConstructor();
}
GreetingPerson.prototype = clone(Person.prototype);
GreetingPerson.prototype.contructor = GreetingPerson;
var manuel = new GreetingPerson()
manuel.setName('Manuel');
manuel.sayHello();
var world = new GreetingPerson()
world.setName('World');
world.sayHello();
manuel.sayHello();
console.log(manuel.name)​;​

Categories