Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Prototypal inheritance" />
  <meta charset=utf-8 />
  <title></title>
</head>
<body>
</body>
</html>
 
var CreatePerson = function(firstName) {
  // revealing pattern
  return {
    walk: walk,
    sayHello: sayHello
  };
  
  function walk () {
    console.log("I am walking!");    
  }
  
  function sayHello () {
    console.log("Hello, I'm " + firstName);
  }
};
// testing the factory
var girl = CreatePerson("Jane");
girl.walk();
girl.sayHello();
var CreateStudent = function(firstName, subject) {
  var person = CreatePerson(firstName);
  var newMethods = {
   sayHello: function(){
      console.log("Hello, I'm " + firstName + ". I'm studying " + subject + "."); 
   },
    sayGoodBye: function() {
      console.log("Goodbye!");
    }    
  };
  
  // start with {} so person is not mutated
  var student = Object.assign({}, person, newMethods);
  
  return Object.create(student);
};
// testing the factory
var student = CreateStudent("Janet", "Applied Physics");
student.sayHello();
student.sayGoodBye();
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers