Last active
October 11, 2015 22:58
-
-
Save maxidr/3932950 to your computer and use it in GitHub Desktop.
Private attributes and functions in javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1 id="qunit-header">Unit Tests</h1> | |
<h2 id="qunit-banner"></h2> | |
<div id="qunit-testrunner-toolbar"></div> | |
<ol id="qunit-tests"></ol> | |
<div id="qunit-fixture"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This shown how to use private attributes and private functions in javascript. | |
From the page of Douglas Crockford http://www.crockford.com/javascript/private.html | |
*/ | |
function Person(attrs) { | |
// Private attributes | |
var first_name = attrs.firstName; | |
var last_name = attrs.lastName; | |
// Private method | |
function complete_name() { | |
return first_name + " " + last_name; | |
} | |
// Privileged method | |
this.name = complete_name; | |
} | |
var john = new Person({ | |
firstName: 'John', | |
lastName: 'Doe' | |
}); | |
test("the function name() must be public (accessible from outside)", function() { | |
equal(john.name(), 'John Doe'); | |
}); | |
test("the attribute first_name must be private", function() { | |
equal(john.hasOwnProperty('first_name'), false); | |
}); | |
test("the function complete_name() must be private", function() { | |
equal(john.hasOwnProperty('complete_name'), false); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Private attributes and functions in javascript | |
description: This shown how to use private attributes and private functions in javascript. From the blog of Douglas Crockford (http://www.crockford.com/javascript/private.html) | |
authors: | |
- Maxi Dello Russo | |
resources: | |
- http://code.jquery.com/qunit/qunit-1.10.0.js | |
- http://code.jquery.com/qunit/qunit-1.10.0.css | |
normalize_css: no |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsfiddle.net/gh/gist/mootools/1.2/3932950/