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>
 
/*
This is a demonstration of different ways to define a method, that is, a function that is a property on an object.
*/
var funcInVar = function(name){
  return "Hello, "+name+"!";
};
// These methods are all completely equivalent!
var obj = {
  
  // a function saved in a variable
  greet1: funcInVar,
  
  // normal inline anonymous function
  greet2: function(name){
    return "Hello, "+name+"!";
  },
  
  // concise method definition (ES6)
  greet3(name) {
    return "Hello, "+name+"!";
  },
  
  // arrow function (ES6)
  greet4: (name)=> {
    return "Hello, "+name+"!";
  },
  
  // single statement arrow function with implicit return
  greet5: (name)=> "Hello, "+name+"!",
  
  // as above but with parens
  greet6: (name)=> (
    "Hello, "+name+"!"
  )
};
console.log( obj.greet1("Steve") );
console.log( obj.greet2("Joe")   );
console.log( obj.greet3("Frank") );
console.log( obj.greet4("Dave")  );
console.log( obj.greet5("Moe")   );
console.log( obj.greet6("Doc")   );
Output

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

Dismiss x
public
Bin info
krawallerpro
0viewers