Skip to content

Instantly share code, notes, and snippets.

@scarfunk scarfunk/index.html
Created Jul 18, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.