Answers for "function prototype javascript"

13

javascript prototype explained

/* Answer to: "javascript prototype explained" */

/*
  The prototype object is special type of enumerable object to
  which additional properties can be attached to it which will be
  shared across all the instances of it's constructor function.

  So, use prototype property of a function in the above example
  in order to have age properties across all the objects as
  shown below:
*/

function Student() {
    this.name = 'John';
    this.gender = 'M';
}

Student.prototype.age = 15;

var studObj1 = new Student();
alert(studObj1.age); // 15

var studObj2 = new Student();
alert(studObj2.age); // 15
Posted by: Guest on April-18-2020
11

function prototype javascript

function Person(name) {
  this.name = name;
}
Person.prototype.getName = function() {
  return this.name;
}

var person = new Person("John Doe");
person.getName() //"John Doe"
Posted by: Guest on June-02-2020
1

prototype in javascript example

//simply prototype means superclass

function Student(name, age) {
  this.name = name,
  this.age = age;
}

var stu1 = new Student("Wasi", 20);
var stu2 = new Student("Haseeb", 25);
//this class is add in all of the objects
Student.prototype.class = "Free Code Camp";

console.log(stu1);
console.log(stu2);
Posted by: Guest on August-04-2021
2

prototype javascript

function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}

Person.prototype.nationality = "English";

var myFather = new Person("John", "Doe", 50, "blue");
console.log("The nationality of my father is " + myFather.nationality)
Posted by: Guest on October-22-2020
2

prototype javascript

/*Prototype is used to add properties/methods to a 
constructor function as in example below */

function ConstructorFunction(name){
	this.name = name //referencing to current executing object
}
ConstructorFunction.prototype.age = 18
let objectName = new ConstructorFunction("Bob")
console.log(objectName.age) //18
Posted by: Guest on August-07-2021
0

function prototype

// function prototype
void add(int, int);

int main() {
    // calling the function before declaration.
    add(5, 3);
    return 0;
}

// function definition
void add(int a, int b) {
    cout << (a + b);
}
Posted by: Guest on May-03-2021

Code answers related to "function prototype javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language