vendredi 6 juin 2014

Questions about prototypes and method chaining


Vote count:

0




Not sure what I am doing wrong but I keep getting the following message in my console. Any guidance would be greatly appreciated. Also if you guys have a better suggestion on how to handle this please let me know!


Uncaught ReferenceError: squareElement is not defined



var animEngine = {};

animEngine.init = function () {
'use strict';

var elem = function(){};

elem.prototype = {
objType : '',
objClass : '',
objBackground: '',
objWidth: '',
objHeight: ''
}

var squareElem = Object.create(elem, {
objType : {value: 'div'},
objClass : {value: 'squareElement'},
objBackground : {value: 'red'},
objWidth : {value: '200px'},
objHeight : {value: '200px'}
});

Function.prototype.method = function (name, fn) {
this.prototype[name] = fn;
return this;
}

squareElement.
method('createObj', function () {
this.element = document.createElement(this.objType);
document.getElementsByTagName('body')[0].appendChild(this.element);
this.element.className = this.objClass;
this.element.style.height = this.objHeight;
this.element.style.width = this.objWidth;
this.element.style.backgroundColor = this.objBackground;

return this;
}).
method('moveObj', function (x,y) {
this.element.style.position = 'relative';
this.element.style.left = x + 'px';
this.element.style.top = y + 'px';

return this;
});

squareElement.createObj().moveObj('100px', '100px');
};


animEngine.init();


asked 40 secs ago






Aucun commentaire:

Enregistrer un commentaire