Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  
</body>
</html>
 
// I represent a friend.
function Friend( name, age ) {
 
    // Store properties.
    var firstName = getFirstName( name );
 
    // Return the public API to the component.
    return({
        sayHello: sayHello
    });
 
 
    // ---
    // PUBLIC METHODS.
    // ---
 
 
    // I return a greeting.
    function sayHello() {
 
        return( "Hello, I am " + firstName + "; I am " + age + "." );
 
    }
 
 
    // ---
    // PRIVATE METHODS.
    // ---
 
 
    // I get the first name, delimited by white-space characters.
    function getFirstName( fullName ) {
 
        return( fullName.split( " " )[ 0 ] );
 
    }
 
}
a = new Friend('Abe', 20);
b = new Friend('Burt', 30);
console.log('strict equality', a.sayHello === b.sayHello);
console.log('loose equality', a.sayHello == b.sayHello);
console.log('instance checking', (a instanceof Friend));
//=================
var protoFriend = function( name, age ) {
  this.firstName = this.getFirstName( name );
  this.age = age;
};
protoFriend.prototype = {
  getFirstName: function( fullName ) {
    return( fullName.split( " " )[ 0 ] );
  },
  sayHello: function() {
    return( "Hello, I am " + this.firstName + "; I am " + this.age + "." );
  }
};
pa = new protoFriend('Abe');
pb = new protoFriend('Burt');
console.log('strict equality', pa.sayHello === pb.sayHello);
console.log('loose equality', pa.sayHello == pb.sayHello);
console.log('instance checking', (pa instanceof protoFriend));
Output 300px

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

Dismiss x
public
Bin info
anonymouspro
0viewers