Skip to content

Instantly share code, notes, and snippets.

@scarfunk
Created July 18, 2017 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scarfunk/381d7c3c9ff8158468278bb716100768 to your computer and use it in GitHub Desktop.
Save scarfunk/381d7c3c9ff8158468278bb716100768 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/vemuqeyodo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
console.clear();
// define the Person Class
function Person() {}
Person.prototype.walk = function(){
var that = this;
that.constructor.prototype.sayGoodBye(); // calling Subclass method.
console.log ('I am walking!');
};
Person.prototype.sayHello = function(){
console.log ('hello');
};
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);
}
// inherit Person
Student.prototype = Object.create(Person.prototype);
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
// replace the sayHello method
Student.prototype.sayHello = function(){
console.log('hi, I am a student');
}
// add sayGoodBye method
Student.prototype.sayGoodBye = function(){
console.log('goodBye');
}
// var person1 = new Person();
// person1.walk(); //ERR! when call parent subclass method does not work.
var student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();
console.log(Person.prototype.constructor.prototype); // this is not contain subclass method.
</script>
<script id="jsbin-source-javascript" type="text/javascript">console.clear();
// define the Person Class
function Person() {}
Person.prototype.walk = function(){
var that = this;
that.constructor.prototype.sayGoodBye(); // calling Subclass method.
console.log ('I am walking!');
};
Person.prototype.sayHello = function(){
console.log ('hello');
};
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);
}
// inherit Person
Student.prototype = Object.create(Person.prototype);
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
// replace the sayHello method
Student.prototype.sayHello = function(){
console.log('hi, I am a student');
}
// add sayGoodBye method
Student.prototype.sayGoodBye = function(){
console.log('goodBye');
}
// var person1 = new Person();
// person1.walk(); //ERR! when call parent subclass method does not work.
var student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();
console.log(Person.prototype.constructor.prototype); // this is not contain subclass method.
</script></body>
</html>
console.clear();
// define the Person Class
function Person() {}
Person.prototype.walk = function(){
var that = this;
that.constructor.prototype.sayGoodBye(); // calling Subclass method.
console.log ('I am walking!');
};
Person.prototype.sayHello = function(){
console.log ('hello');
};
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);
}
// inherit Person
Student.prototype = Object.create(Person.prototype);
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
// replace the sayHello method
Student.prototype.sayHello = function(){
console.log('hi, I am a student');
}
// add sayGoodBye method
Student.prototype.sayGoodBye = function(){
console.log('goodBye');
}
// var person1 = new Person();
// person1.walk(); //ERR! when call parent subclass method does not work.
var student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();
console.log(Person.prototype.constructor.prototype); // this is not contain subclass method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment