[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Why does this not work: function foo() { this.bar = function()

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 10
Thread images: 3

File: dog doing javascript.jpg (55KB, 512x501px) Image search: [Google]
dog doing javascript.jpg
55KB, 512x501px
Why does this not work:

function foo() {
this.bar = function() {
console.log('a');
};
}

foo.bar(); // TypeError: Cannot find function bar in object function foo() {...}


And this works:

function foo() {}

foo.bar = function() {
console.log('b');
};

foo.bar(); // prints b
>>
>>58837624
>javascript
that's why
>>
Can we just like... ban Javascript or something?
>>
u need to call the contructor function to use this.foo

var baz = new foo();
baz.bar();
also you should put your funcion in a var
>>
the first one:
"have a function foo, that, when called, sets this.bar to something"
you're not calling the function, so foo.bar can't exist

the second one:
"define function foo, define foo.bar"
these are separate steps that are executed immediately
>>
>>58837624
If you couldn't figure that out, programming is not for you
>>
File: 1353513031676.gif (1MB, 200x144px) Image search: [Google]
1353513031676.gif
1MB, 200x144px
>javascript
>>
>>58837700
What the fuck did you just fucking type about me, you little bitch? I’ll have you know I graduated top of my class at MIT, and I’ve been involved in numerous secret raids with Anonymous, and I have over 300 confirmed DDoSes. I am trained in online trolling and I’m the top hacker in the entire world. You are nothing to me but just another virus host. I will wipe you the fuck out with precision the likes of which has never been seen before on the Internet, mark my fucking words. You think you can get away with typing that shit to me over the Internet? Think again, fucker. As we chat over IRC I am tracing your IP with my damn bare hands so you better prepare for the storm, maggot. The storm that wipes out the pathetic little thing you call your computer. You’re fucking dead, kid. I can be anywhere, anytime, and I can hack into your files in over seven hundred ways, and that’s just with my bare hands. Not only am I extensively trained in hacking, but I have access to the entire arsenal of every piece of malware ever created and I will use it to its full extent to wipe your miserable ass off the face of the world wide web, you little shit. If only you could have known what unholy retribution your little “clever” comment was about to bring down upon you, maybe you would have held your fucking fingers. But you couldn’t, you didn’t, and now you’re paying the price, you goddamn idiot. I will shit code all over you and you will drown in it. You’re fucking dead, kiddo.
>>
>>58837679
wrong, bar is the function, not the result of its call. the problem comes from not using new
>>
File: dart.png (31KB, 1200x900px) Image search: [Google]
dart.png
31KB, 1200x900px
>>58837624
What the world needs is this. It's not catching up because the web dev industry is filled with bootcamp retards who fall for the latest JS framework fad.
Thread posts: 10
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.