ded / klass

a utility for creating expressive classes in JavaScript
751 stars 78 forks source link

Klass.js doesn't work #8

Closed matthew-dean closed 13 years ago

matthew-dean commented 13 years ago

As soon as Google Chrome comes across the Klass file, it just spits out the error "TypeError: undefined is not a function." It's also not working in Internet Explorer.

ded commented 13 years ago

it works fine for me in both browsers. both main src and minified. are you using this build from github?

matthew-dean commented 13 years ago

Yep. Sure am. I went as far as to disable every piece of JavaScript on the page that loaded before the Klass library. Google Chrome gives the error above. IE says "Object expected." This is before USING the Klass library. It simply won't evaluate. I've used both the min and regular versions.

I even went back and got it again from GitHub in case there was a weird copy / paste issue. Wait a minute.... why are there no semi-colon terminators?

https://raw.github.com/ded/klass/master/klass.js

matthew-dean commented 13 years ago

Firefox (w/ Firebug) is more verbose in its error (Note the "is not a function" at the end):

function (name, definition) {if (typeof define == "function") {define(definition);} else if (typeof module != "undefined") {module.exports = definition();} else {this[name] = definition();}}("klass", function () {var context = this, old = context.klass, f = "function", fnTest = /xyz/.test(function () {xyz;}) ? /\bsupr\b/ : /.*/, noop = function () {}, proto = "prototype", isFn = function (o) {return typeof o === f;}; function klass(o) {return extend.call(isFn(o) ? o : noop, o, 1);} function wrap(k, fn, supr) {return function () {var tmp = this.supr;this.supr = supr[proto][k];var ret = fn.apply(this, arguments);this.supr = tmp;return ret;};} function process(what, o, supr) {for (var k in o) {if (o.hasOwnProperty(k)) {what[k] = isFn(o[k]) && isFn(supr[proto][k]) && fnTest.test(o[k]) ? wrap(k, o[k], supr) : o[k];}}} function extend(o, fromSub) { function noop() {} noop[proto] = this[proto];var supr = this, prototype = new noop, isFunction = isFn(o), _constructor = isFunction ? o : this, _methods = isFunction ? {} : o, fn = function () {if (this.initialize) {this.initialize.apply(this, arguments);} else {fromSub || isFunction && supr.apply(this, arguments);_constructor.apply(this, arguments);}};fn.methods = function (o) {process(prototype, o, supr);fn[proto] = prototype;return this;};fn.methods.call(fn, _methods).prototype.constructor = fn;fn.extend = arguments.callee;fn[proto].implement = fn.statics = function (o, optFn) {o = typeof o == "string" ? function () {var obj = {};obj[o] = optFn;return obj;}() : o;process(this, o, supr);return this;};return fn;} klass.noConflict = function () {context.klass = old;return this;};context.klass = klass;return klass;}) is not a function

I'd be grateful if you re-opened this issue and took a look.

matthew-dean commented 13 years ago

Ohhhhhhhhhhh......

So, normally, what I do is follow the standard practice of minimizing my functions and putting them into a plugins.js file, rather than give them individual script references. I've been doing this since I started using HTML5 boilerplate and it minimizes server requests.

I was baffled because I was copying the code EXACTLY and pasting it into my plugins.js file.

The problem is, your function doesn't terminate. It has an implied termination only because it's the only executable line in the file. Any other JavaScript, and the whole thing fails.

Can you add a terminating semi-colon for fools like me? :-)

ded commented 13 years ago

you could always insert a semi-colon before your code. this is valid code:

; var foo = 'bar'
matthew-dean commented 13 years ago

Yes, exactly. I inserted a semi-colon myself. I'm just saying it was a weird problem to track down. It might be nice if you could copy/paste without modification into other scripts.

leeoniya commented 13 years ago

also beaten to death here: https://github.com/mishoo/UglifyJS/issues/126

matthew-dean commented 13 years ago

Ugh. Thanks leeoniya. That's an interesting thread, wish I'd seen it earlier. Seems obvious that including the semi-colon is just a far better option in terms of usable scripts.

saran110313 commented 8 years ago

function klass() { this.initialize.apply(this, arguments); } how to solve Uncaught TypeError: Cannot read property 'apply' of undefined this problem...

saran110313 commented 8 years ago

I'm receiving this error originating from fabric.js

Uncaught TypeError: Cannot read property 'apply' of undefined

function klass() { (ln: 1690) --> this.initialize.apply(this, arguments); }

ded commented 8 years ago

@saran110313 it's hard to tell where the real problem is without more context.

saran110313 commented 8 years ago

function klass() { (In: 1690)--> this.initialize.apply(this, arguments); }

Uncaught TypeError: Cannot read property 'apply' of undefined i m working nodejs with fabric.js.