Permalink
Browse files

Added private/public method example

  • Loading branch information...
1 parent 66e4d0d commit 3433d40854bfd8db73d571a3a80270fb8fdb930f @adomokos committed Oct 11, 2010
View
@@ -11,11 +11,13 @@
<!-- include source files here... -->
<!-- <script type="text/javascript" src="src/ProJS.js"></script> -->
- <script type="text/javascript" src="src/Chapter03.js"></script>
+ <!-- <script type=text/javascript src="src/Inheritance.js"></script> -->
+ <script type="text/javascript" src="src/PublicPrivate.js"></script>
<!-- include spec files here... -->
<!-- <script type="text/javascript" src="spec/ProJSSpec.js"></script> -->
- <script type="text/javascript" src="spec/Chapter03Spec.js"></script>
+ <!-- <script type="text/javascript" src="spec/InheritanceSpec.js"></script> -->
+ <script type="text/javascript" src="spec/PublicPrivateSpec.js"></script>
</head>
<body>
@@ -1,4 +1,4 @@
-describe("Chapter03", function() {
+describe("Inheritance", function() {
it('has a Person object with a getName() method', function() {
var user = new Person("John");
expect(user.getName()).toEqual("John");
@@ -0,0 +1,13 @@
+describe("PublicPrivate", function() {
+ it('has a public accessor to the private name field', function() {
+ var user = new User();
+ user.setName("John");
+ // a private method add " Changed" to the name
+ expect(user.getName()).toEqual("John Changed");
+ });
+
+ Xit('hides the private method', function() {
+ var user = new User();
+ expect(user.privateMethod()).toThrow(e);
+ });
+});
File renamed without changes.
View
@@ -0,0 +1,17 @@
+function User() {
+ var name;
+
+ var privateMethod = function() {
+ name += " Changed";
+ };
+
+ return {
+ setName: function(newName) {
+ name = newName;
+ privateMethod();
+ },
+ getName: function() {
+ return name;
+ }
+ };
+}

0 comments on commit 3433d40

Please sign in to comment.