Created
July 18, 2017 12:46
-
-
Save scarfunk/381d7c3c9ff8158468278bb716100768 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/vemuqeyodo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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