Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <p data-bind="text: kodog.greeting"></p>
</body>
</html>
 
// Plain JS, works as expected
// ###########################
var Animal = function() {
  var self = this;
  
  self.hello = function() {
     return 'Not implemented';
  };
  
  self.greeting = function() {
    return self.hello() + '!!';
  };
};
var dog = new Animal();
dog.hello = function() {
  return 'Woff';
};
console.log(dog.greeting());
// Knockout
// ########
var KOAnimal = function() {
  var self = this;
  
  self.hello = ko.observable(ko.computed(function() {
    return 'KO Not implemented';
  }));
  
  self.greeting = ko.computed(function() {
    return self.hello()() + '!!';
  });
};
var kodog = new KOAnimal();
kodog.hello(ko.computed(function() {
  return 'KOWooff';
}));
console.log(kodog.greeting());
Output

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

Dismiss x
public
Bin info
anonymouspro
0viewers