Skip to content

Instantly share code, notes, and snippets.

@maxidr
Last active October 11, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxidr/3932950 to your computer and use it in GitHub Desktop.
Save maxidr/3932950 to your computer and use it in GitHub Desktop.
Private attributes and functions in javascript.
<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 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);
});
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
@maxidr
Copy link
Author

maxidr commented Oct 22, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment