Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Prototypical Model
- var UserPrototype = {};
- UserPrototype.constructor = function(name)
- {
- this._name = name;
- };
- UserPrototype.getName = function()
- {
- return this._name;
- };
- var user = Object.create(UserPrototype);
- user.constructor('Joe Bloggs');
- console.log('user = ' + user.getName());
- var AdminPrototype = Object.create(UserPrototype);
- AdminPrototype.getName = function()
- {
- return UserPrototype.getName.call(this) + ' [Admin]';
- };
- var admin = Object.create(AdminPrototype);
- admin.constructor('Sally Ann');
- console.log('admin = ' + admin.getName());
- // Classical Model
- function User(name)
- {
- this._name = name;
- }
- User.prototype.getName = function()
- {
- return this._name;
- }
- var user = new User('Joe Bloggs')
- console.log('user = ' + user.getName());
- function Admin(name)
- {
- User.call(this, name);
- var secret = 'Hello';
- this.getSecret = function(password)
- {
- return (password === 'cheese')
- ? secret
- : 'It\'s a secret!';
- }
- }
- Admin.prototype = Object.create(User.prototype);
- // Admin.prototype = new User();
- Admin.prototype.constructor = Admin;
- Admin.prototype.getName = function()
- {
- return User.prototype.getName.call(this) + ' [Admin]';
- };
- var admin = new Admin('Sally Ann');
- console.log('admin = ' + admin.getName());
- console.log('secret = ' + admin.getSecret('cheese'));
- function Student(properties)
- {
- var $this = this;
- for (var i in properties) {
- (function(i)
- {
- $this['get' + i] = function()
- {
- return properties[i];
- }
- }(i));
- }
- }
- var student = new Student({
- Name: 'Joe Bloggs',
- Age: 24
- });
- console.log(student.getName());
- console.log(student.getAge());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement