Functions Objects and Scope


Functions Objects and Scope

[[--Word of Advice-- Understanding this stuff is critical, spend time on it . Asking and answering questions  is very likely to be helpful in clearing up doubts.--]]

[[--Word of Warning-- You will read a lot of rubbish on the internet like "Crockford says 'do this/don't do that, Crockford is right/wrong, functional/oo is better/worse than oo/functional, x/y is better/worse than y/x"
Ignore it, learn the concepts for yourself and develop your own style always with the idea "use what is best for the problem you have". You can pick sides later.--]]

Remember we described "alert" like this:

"The program consists of a statement which is a call to the function that is the value of the property named alert of the global object."

So we are going to clarify what all this means assuming by now that you are comfortable with the idea of a statement.

Basically a function is a piece of reusable code formed like this:

function doSomething(withSomeArguments) {
operateOnArguments;
return theResult; }

or to take a more concrete example

function square(x) {
return x*x; }

and you would call this function with, for example, square(6), returning 36.

See if you can put together a function (base, exponent) that returns "base" to the power "exponent" eg 2 to the 5th power, which would be 32.

Now watch this video from http://goo.gl/XT9FG and read Chapter 3 from Eloquent Javascript, http://goo.gl/4DyqX (the latter has the answer to the above power problem).

Before we go further we need to talk about



Scope

Scope refers to where variables and functions are accessible (visible), and in what context they are being executed (execution context). Basically, a variable or function can be defined in a global or local scope.


Global scope

When something is global means that it is accessible from anywhere in your code. Take this for example:

function greetVisitor () {
    return alert("Howdy Doodee");
}

OR

var greeting = "Howdy Doodee";

If this is run in a browser, the function or the variable scope would be window, making it visible to everything running in the browser window.

Local scope

function greetVisitor () {
var greeting = "Howdy Doodee";
return alert (greeting); }

and now greeting is not visible outside the function (if you omitted the reserved var then it would be global which you probably don't want; if there is a global var with the same name, the function will use the local one).

All javascript code is executed in an execution context. Global code (code executed inline, normally as a js file, or html page, loads) gets executed in global execution context, and each invocation of a function has an associated execution context.

When an execution context is created a number of things happen in a defined order. First, in the execution context of a function, an "Activation" object is created.

The next step in the creation of the execution context for a function call is the creation of an arguments object, which is an array-like object with integer indexed members corresponding with the arguments passed to the function call, in order. A property of the Activation object is created with the name "arguments" and a reference to the arguments object is assigned to that property.

Next the execution context is assigned the scope. A scope consists of a list (or chain) of objects. Each function object has an internal scope property that also consists of a list (or chain) of objects. The scope that is assigned to the execution context of a function call consists of the list referred to by the scope property of the corresponding function object with the Activation object added at the front of the chain (or the top of the list).

Closures

A closure is a function together with its scope; when a function is invoked, a call object encapsulating the Arguments object for the function together with named parameters and local variables, is added to the front of the scope chain.

Ordinarily, when the function exits, the call object is removed from the scope chain even if there is a nested function, provided that the nested function is only utilized in the outer function.

However, if the nested function is the return value of the outer or the nested is stored as a property of some other object, then the call object is not removed and the nested keeps its reference to it.

This last possibility is what people usually mean when they refer to (make a song and dance about) closures; in general though, closures in Javascript are simply a result of lexical scoping (meaning that functions run in the scope in which they are defined, not the scope from which they are executed.)

With the above in mind, work your way through this bit of code and see if you can understand what is happening:

function add (x) {
	return function (y) {
		return x + y;
	};
}
var add5 = add(5);
var no8  = add5(3);
alert(no8); // Returns 8  (paste it into jsbin to check).

When you consider the possibilities for variable name conflicts in the global scope, one idea that suggests itself is to make as many of your variables local as you can; creating your own namespaces is more advanced javascript and you can search on the web if you are interested.

OK, that takes care of the "function" bit of our alert statement.

"The program consists of a statement  which is a call to the function that is the value of the property named alert of the global object..."

We have that:-

  • A program/script is a collection of communicating objects.
  • An object has a collection of properties.
  • A property has a name, a value, and a collection of attributes.
  • The value of a property is a primitive value or an object.

That's it. Things that seem like basic concepts, like functions, arrays, and regular expressions are also kinds of objects. The only objects that are distinctive are functions in that they take parameters, can hold executable code and point to scopes.

Objects have properties. Access the properties with either dot or square bracket notation.  You can even delete properties.

var x = {};
x.age = 17;
x.height = 65.3;
var score = x.age * x["height"];
var z = {age: 30, color: "red", total: 3};
z.last = true;
var rat = {triple: {a:4, b:undefined, c:{4:null}}, 7: "stuff"};
delete z.age;
Every object in JavaScript has a prototype. When looking up properties,the prototype
chain is searched. You can only set the prototype when the object is created.
In newer JavaScript versions, you do this with Object.create:
 
var protoCircle = {x: 0, y: 0, radius: 1, color: "black"};

var c1 = Object.create(protoCircle);

c1.x = 4;   c1.color = "green";           // Now c1.y === 0 and c1.radius === 1

The special keyword this has one of four different meanings.
  1. If used in a global scope, it refers to the global object.
  2. If used in a function called via Function.apply or Function.call, the value for this was passed in.
  3. If used in a function invoked with the new operator it refers to the object being created.
  4. If used in a function called normally, it refers to the object the function was called through (then the function is called a method).

So what about the "global object"?

All global variables and functions become properties of the global object. In browsers. the window object doubles as the global object, and most developers use it as such without even realizing.

http://goo.gl/galYA   OO Javascript

http://goo.gl/clHFJ
    Core Javascript

http://goo.gl/z9TZ
    More about Objects

http://goo.gl/ojBl
      More about Objects 2



Reflections/Homework

 

  1. What do we mean when we say a variable in a function is shadowing a top level variable?

  2. A recursive function, must have some sort of an end condition. Why would we get a "out of stack space" error message if a recursive function does not have an end condition?

  3. Reflect about the difference between object inheritance and class inheritance

  4. What is object augmentation, and how do we do it?

  5. There is a way to add a method to String, such as any new String we create will have that augmented method (this is a bit different from object augmentation). How would you do this?

  6. What is the difference between an array and an object.

  7. Exercises 3.1 and 3.2 from chapter 3 of Eloquent Javascript (here...)

  8. Write up in your own words the connection between execution context, scope and function closures.

  9. Shown below is some code which does something useful. The function 'iterateAndOperate' is the one which accomplishes something useful. The remaining code helps this function. Try to understand what the function accomplishes and solve the problems in part a, b, and c. The code can be done inside the console in Javascript, or in the web browser. Please see this comment, for hints on how you may do it inside a web page(remember, HTML has special codes for spaces and newlines).

    1. Use the function iterateAndOperate to draw an image which looks like this
    2. Use the function iterateAndOperate to draw a triangle which looks like this
    3. In your code which invokes iterateAndOperate() without any parameters, as shown. An Exception will be thrown. Catch the Exception show an Alert to the user with a user friendly error message.
//A constant to hold the String "null". To be used in typeof checks
NULL_VAL = "null";
//A constant to hold the String "undefined". To be used in typeof checks
UNDEFINED_VAL = "undefined";

/*
* This function checks if the specified parameter is null or undefined
* @param something The specified parameter to check for a null or undefined value
* @param name The name of the parameter. This will be used in the error message
* If the value 'something' is found to be null or undefined, then this method
* will throw an Error
*/
function checkNullOrUndefined(something, name) {
if(UNDEFINED_VAL == typeof(something)) {
throw new Error(name + " cannot be undefined");
}
if(NULL_VAL == typeof(something)) {
throw new Error(name + " cannot be null");
}
}


/*
* This function accepts an array object and a function reference.
* It iterates through the array and invokes the specified function
* for every element of the array. In each invocation the current
* array element is given to the function as a parameter.
* @param arr The array
* @param func The function to invoke for every element of the array
* This method does not return any specific value.
* This method throws an Error if 'arr' is null, undefined, or is not an array
* This method throws an Error if 'func' is null, undefined, or is not a function
*/
function iterateAndOperate(arr, func) {
checkNullOrUndefined(arr, "arr");
checkNullOrUndefined(func, "func");
// Verify that arr is an array
if(!(arr instanceof Array)) {
throw new Error("arr does not seem to be an array");
}
// Verify that arr is an array
if("function" != typeof(func)) {
throw new Error("func is not a function");
}
for(var i=0; i<arr.length; i++) {
func(arr[i]);
}
}

Discusión de la Tarea


  • extravaganzasd   15 de mayo de 2011 a las 23:07

    I too have been working my way through the week 2 material and am glad to hear that the timing of this course is more "flexible".   This is a side thought but I wonder if I should go back and take math classes.  The eloquent javascript author keeps mentioning functions that you remember from your math classes and it has been so long since I took algebra (and never got to calculus) that i wonder  if I need this skill set.  (Not really a question - I am just musing out loud.)

     

    I know that this whole thing is a beta, so all of the moving around is a necessary evil but it has been confusing.  The one thing that was the hardest for me in week one was clicking from the forums to everyone's individual pages.  Altouggh it probably isn't possible it would be nice to read everyone's text answers in a thread and be able to reply directly to them as a conversation.

     

  • Anónimo   14 de mayo de 2011 a las 06:49

    I'm still waiting for anyone to put up any Task about some aspect of Javascript which they think should have more attention or material or exercises or whatever......

    Also, everybody is talking about Week2, does that mean that Week1 was "too easy" or it was "just right" or what?

    For instance, it needs to be explained why less than half of participants did the Week1 assignments? Is this just a shortage of time?

    Does a 6 Week course need to be a 12 week course? Or even longer? And cover less material?

    Answers on a postcard, pleasesmiley

  • Jeffrey Yancey   14 de mayo de 2011 a las 07:08
    En Respuesta A:   Anónimo   14 de mayo de 2011 a las 06:49

    Well, I might have been the only ignorant participant in the class, or perhaps it was due to my week late start, but I was completely unsure where I was to post the link to my week 1 assignment. Even at this moment I am unsure that I posted in the correct location. We have the original P2PU site, the new beta, the Zoho forums. I must have missed it, but for the life of me I couldn't locate the "official" means by which to submit my weekly assignments.

  • Jeffrey Yancey   14 de mayo de 2011 a las 07:09
    En Respuesta A:   Anónimo   14 de mayo de 2011 a las 06:49

    Well, I might have been the only ignorant participant in the class, or perhaps it was due to my week late start, but I was completely unsure where I was to post the link to my week 1 assignment. Even at this moment I am unsure that I posted in the correct location. We have the original P2PU site, the new beta, the Zoho forums. I must have missed it, but for the life of me I couldn't locate the "official" means by which to submit my weekly assignments.

  • Anónimo   14 de mayo de 2011 a las 07:31
    En Respuesta A:   Jeffrey Yancey   14 de mayo de 2011 a las 07:09

    @Jeffrey

    Yes, there has been confusion because the course started at old.p2pu and in a Google container and is now switching to the new.p2pu and dropping the Google container (which includes Zoho and Google sites). So from now on, everything is at new.p2pu

    Still, considering that you started late, you are to be congratulated on actually doing your Week1 assignmentsmiley

    To answer your question more directly, this thread we are in is the comment thread for the Week2 Task.

    To be "correct" (now), you should post a link to your assignment in the Week1 Task thread; this link can be to the Google site or to any other blog or site that you choose.

  • Jeffrey Yancey   14 de mayo de 2011 a las 07:33
    En Respuesta A:   Anónimo   14 de mayo de 2011 a las 07:31

    Will do. Having everything centralized here should help with issues like this in the future, I imagine. Is that the current direction things are headed?

  • Anónimo   14 de mayo de 2011 a las 07:42
    En Respuesta A:   Jeffrey Yancey   14 de mayo de 2011 a las 07:33

    Yes that's right, old.p2pu is going to wind down (I think new course creation there is currently being disabled).

    New.p2pu is a Beta (seems like everything is these days) and there is less emphasis on "courses" and more emphasis on "p2p" (learning groups) and within those,a focus on "Tasks" (smaller sized pieces of things to be completed).

    Anyone can create such a group; for example, let's say some participants in this course wanted to set up a group learning, I don't know, js frameworks or one of them, then they could do that and then others could join in as followers or participants.

    Of course this whole set up relies on active p2p-style behaviour to work properly.

  • brotherhutch   14 de mayo de 2011 a las 14:10
    En Respuesta A:   Anónimo   14 de mayo de 2011 a las 06:49

    What I think would work well is the approach another online course I was in took: completed assignments are not posted to a forum or group; instead there is a centralized, easily accessible list of everyone's course page (our Google Sites in this case), and everyone updates their personal pages with their work on a schedule. The organiser(s) and learners with some expertise visit this list and provide feedback on the personal pages as time allows, and/or when learners request that someone have a look at their code because they have specific questions about an assignment.

  • Anónimo   14 de mayo de 2011 a las 14:30
    En Respuesta A:   brotherhutch   14 de mayo de 2011 a las 14:10

    That was basically the approach taken in the previous (easier) version of this course, someone even set up an rss aggregation of all the assignment postings.

    Although it started off OK, it didn't work, the drop out rate was terrible and by the last 2 weeks, hardly anybody was around ( I was the only one to even try the Javascript Badge and had to go hunting for my votes up!).

    There was also a large initial participation so they divided it into 2 tracks to include an even easier track for people without any programming. This easier track collapsed half way through and the ones that were left joined in the main track.

    The truth seems to be that people are either unable or unwilling to commit to a fixed time schedule regardless of the content. This only seems to work when people are forced to adhere to a schedule by an external force eg need to get a qualification or perhaps when they have paid for a course and want to get their money's worth.

    On the other side of the coin (this side), if you take away the "teacher", the course designer, the admin person (and the fees), what is left to make it work except the participants themselves?

    I'm inclined to think the current education system doesn't deliver as it should, however it is still a bit unclear as to how that problem should be dealt with.

  • Parag   15 de mayo de 2011 a las 01:54
    En Respuesta A:   Anónimo   14 de mayo de 2011 a las 06:49

    Having taught as well as learnt online, I have some thoughts:

    Facilitating: When we are teaching/facilitating, we are very excited and eager to help the participants of the course learn as much as they possibly can in the duration. This is most likely because we are using that technology/language in production and see all the possibilities, corner cases, caveats, etc, and we want to share all of this with the participants.

    However

    Learning: When we are learning, we are looking at initial hand holding. So if I am learning a new language, I know there are lot's of resources on the Internet, but I don't know where to begin. I don't what I should learn first. I don't know what kind of programming exercises I should do first. I need feedback on the code I am writing, and on the interpretations I am making of the technology in my mind. This is where a facilitator/mentor/teacher can help.

    Course Content:

    About the course content, I agree that we need to understand the needs of a first time learner. I too made a similar error when I taught Javascript last semester. I was so excited about sharing as much as I could, that I did not realize that what I am offering may not be what a learner needs at this stage.

    Suggestions:

    First of all Selfstudier, please excuse me for making suggestions in your course... my intention is not to make you change direction or restructure things... but rather I want to share my experience, having been on both sides of the table.

    What I am writing below are just just suggestions... I would still look up to you for the final call.

    Let us restructure the course so that we are focusing only on fundamental concepts. I know we have progressed well ahead in this course, but that is fine. The JQuery book has a very good introduction to Javascript. here http://jqfundamentals.com/book/#chapter-2

    Selfstudier, is it alright if everyone spends some time revisiting earlier concepts? It would be wonderful if everyone can post on the forums... not only questions but also your thought process, the assumptions and interpretations you are making, whatever doubts/questions some to your mind...

    Those who are already comfortable with this material can perhaps help by answering questions in the forum?

    Once we complete the basics, we should get back to understanding methods, scoping, closures, etc...

    So I guess things would be somewhat like this:

    Week 3 - Revisiting fundamental concepts

    Week 4 - Functions, scope, closures

    Week 5 & 6 - Manipulating the DOM

    Selfstudier has posted some excellent resources to help a learner go beyond the basic concepts. How about doing this... those who feel they can proceed at a faster pace can learn from those resources as well.

    Would like to know everyone's thoughts?

  • Anónimo   15 de mayo de 2011 a las 05:20
    En Respuesta A:   Parag   15 de mayo de 2011 a las 01:54

    I will copy this into the Learning/Study Plans Section and we can discuss it there.

  • (aleb   1 de julio de 2011 a las 15:10
    En Respuesta A:   Anónimo   14 de mayo de 2011 a las 06:49

    I have to work, will catch up this weekend. I do have a question though. I know a variable name can contain a digit, letter, underscore; etc; but why can't a variable name begin with  digit or can it?

    Thanks!

    (aleb

  • Andre Dublin   1 de julio de 2011 a las 19:37
    En Respuesta A:   (aleb   1 de julio de 2011 a las 15:10

    Great question aleb!

    Numbers in javascript are a datatype, either integer value or floating point value.

    These are the datatypes available in javascript: I might be missing one or two

    Number = 1, 2, 102142, 2214.1511

    String = 'a string', "a string"

    Boolean = true, false

    Function = function () {}

    Object = {}

    Array = [1,3,'hey',13, {}]

    Null

    Undefined

    Regular Expressions

    Date Object

    Error Objects

    In javascript these properties of these values are predetermined and cannot be altered.  So we can't say that:

    1 = a

    or

    true = "Hello World"

    Variables in Javascript are primitive types and reference types.  Meaning they hold a value or they point to a value.  Here are some examples

    Primitive Types

    var num = 45

    var decimal = 123.131092

    var b = a

    Primitive types hold actual values

    Reference Types

    var array = [1,2,3]

    var b = a

    a[2] = 3

    Reference types point to or hold a collection of values (basically speaking)

    Hopes this helps!

  • (aleb   1 de julio de 2011 a las 22:45
    En Respuesta A:   Andre Dublin   1 de julio de 2011 a las 19:37

    Thanks Andre. If I understand you correctly, you're basically saying that a variable name that began with a digit would not tell us anything about the value in the variable, and is why we don't begin a variable name with a digit?

    Again, thanks so much!

    (aleb

  • Andre Dublin   1 de julio de 2011 a las 22:49
    En Respuesta A:   (aleb   1 de julio de 2011 a las 22:45

    Oops, I feel like I didn't answer your question completly.

    You cannot start a varialbe name with a number, because Javascript will recognize that as a Datatype, but you can end or mix in numbers in a variable name.  However, may I suggest that you not practice this naming convention because your variables won't be semantic and harder to decipher what they reference or define.  

    This would be why we don't start variables with numbers or include numbers in variables at all.

    Hope this helps

  • (aleb   1 de julio de 2011 a las 22:59
    En Respuesta A:   Andre Dublin   1 de julio de 2011 a las 22:49

    Ah, I see now. Thanks so much for your help!

  • Anónimo   13 de mayo de 2011 a las 18:54

    Code Example

    http://jsbin.com/agoke6/edit

  • Jakub   13 de mayo de 2011 a las 16:48

    Hi all!

    Just like Adam and Krabat, I must say that I found week2 marerial really difficult to digest. But, REALLY interesting at the same time.
    I must admit that I have not event come close to week 2 assignment. All I've been doing is reading, reading, and more reading trying to get to the bottom of scope, closure, execution context and so on...
    Maybe I'm wrong but I can not imagine going any further with the material without having really good understanding of all those crucial ideas covered in week 2.
    I just feel that I need more time to get the hang of it.

    Selfstudier, I just hope you have enough patience for my ignorance.

  • Anónimo   13 de mayo de 2011 a las 17:00
    En Respuesta A:   Jakub   13 de mayo de 2011 a las 16:48

    @Jakub

    With the new system, you can take your time,

    Maybe I won't be answering all the questions from now on, I hope to have some help quite soon (fingers crossed).

    As I said before, if you can understand all the Week 2 material, then you are doing very well.

  • brotherhutch   13 de mayo de 2011 a las 20:34
    En Respuesta A:   Jakub   13 de mayo de 2011 a las 16:48

    I think this is a common sentiment with many here. In a JS book I've been using closures aren't even mentioned until page 338 of a 700 page book. There are many other concepts and coding techniques and much practice to be accomplished in the textbook prior to the kinds of things we encountered in week 2. I also find sources we are using somewhat confusing as they are from different authors using inconsistent jargon and are targeted at various levels of expertise and may repeat information from source to source. It seems rather convoluted and inefficient. I've often found it hard to find answers to the questions when I have to sort through the maze of sources for an answer that uses different terms than those employed in the question. I can see some value in working this out for oneself, but it takes a lot of time which I think many of us probably do not have a whole lot of.

    Anyway, yes more coding practice related to theory would help as we go along. And perhaps a gentler curve into these more advanced ideas. Perhaps in future versions of this course a narrower and more focused set of learning resources could be collected. I'd like to see a JS course that really narrows its scope to current best practices, with more coding examples, and practical parsing exercises -- which I think would actually teach theory more efficiently than asking general questions about it ( when studying a literary language for example, much time is spend breaking down a sentence into its parts, identifying what they are and what they do, and then putting it all back together to understand the meaning).

    I must add though that learning JS along side others in this format is FAR superior to working my way through this lonely big book I have on my own. I value the time and effort put in by the organisers and contributing students. Thanks all! Let's keep it goin!

  • Anónimo   14 de mayo de 2011 a las 05:32
    En Respuesta A:   brotherhutch   13 de mayo de 2011 a las 20:34

    @brotherhutch

    "I'd like to see a JS course that really narrows its scope to current best practices, with more coding examples, and practical parsing exercises"

    I'm sure a lot of people would be very grateful if SOMEONE would organize something like that, are you offeringangle? Would that be a 101 course? Or a 201, 301? (Serious question).

    The situation has become complicated because after being stable for 10 years, there are suddenly a lot of changes and Javascript is evolving into a more fully fledged programming language (Java, for example, is spread out over a couple of years at Uni).

    I have been thinking about it a little bit and it seems to me that prior exposure to programming is now a prerequisite for learning Javascript.

     

  • jandrocamus   14 de mayo de 2011 a las 10:26
    En Respuesta A:   brotherhutch   13 de mayo de 2011 a las 20:34

    I agree completely on everything you said.

    It´s a very good thing this course is happening, but besides learning its a very nice idea to be working on how to improve the next javascript courses.

  • Amanda   14 de mayo de 2011 a las 13:42
    En Respuesta A:   Jakub   13 de mayo de 2011 a las 16:48

    @Jakub

    I couldn't have said it better myself.  I, also have just been reading, reading, reading because I feel, if I want to go forward and understand all the material and terms, I have to get this down first. 

    @Selfstudier

    In no way am I complaining, I just wanted to let you know I am still in, but just taking everything in.  I am sorry if this wasn't the course you expected, but prior to, I have had zero experience programming.  This is a great course with so much valuable information being offered and I think you're doing a wonderful job.  I, too, just hope you will excuse my tardyness in understanding the Week 2 material.

  • brotherhutch   14 de mayo de 2011 a las 13:56
    En Respuesta A:   Anónimo   14 de mayo de 2011 a las 05:32

    I'd love to run such a course! But I don't know JS, which is why I'm here. smiley Perhaps some day.

  • Anónimo   14 de mayo de 2011 a las 14:09
    En Respuesta A:   Amanda   14 de mayo de 2011 a las 13:42

    Under the new system there is less time pressure.

    If you learn the Week2 material carefully and really understand it you will get a very good basis for further study, code that would otherwise be quite difficult to read will suddenly be clear.

    The only thing that I am a bit disappointed about is that I did think that there would be a lot more questions and discussion about Week2 than there has been.

    You could try to separate functions, objects and scope into separate parts to some extent but the truth is, especially with all the changes, the things are all tied up up with each other.

    With hindsight, I think I was being too optimistic in allocating only 1 Week (however, most of the material was available several weeks before the official course start).

  • Anónimo   14 de mayo de 2011 a las 14:14
    En Respuesta A:   brotherhutch   14 de mayo de 2011 a las 13:56

    You can adapt some materials/examples from your 700 page bookcool.

    What is it, the Javascript Bible?

  • brotherhutch   14 de mayo de 2011 a las 14:38
    En Respuesta A:   Anónimo   14 de mayo de 2011 a las 14:14

    It is this one:
    http://www.murach.com/books/mdom/index.htm

    But as you said, JS is changing rapidly now. This text already feels rather dated.  As I work through this course and supplement with the text and other internet sites I am finding marked differences in coding choices made as concepts get more advanced. I shall endeavour to contribute to the Tasks once I get more compfortable with this new system here (and understand a specific aspect of JS well enough to set one up).

    I'm sure the text's TOC could be transposed into an online course syllabus, but I certainly wouldn't be the one to run it. It would be a little like Ashton Kutcher trying to run a course on how to act for the Shakespearean stage -- or just how to act, period.

  • Anónimo   14 de mayo de 2011 a las 14:50
    En Respuesta A:   brotherhutch   14 de mayo de 2011 a las 14:38

    It isn't necessary to "run" things, as you put it (that's an anti-pattern here).

    Your book gives you an edge in Week 3 (arguably anybody that knows jQuery has an edge as well but may not know exactly why).

    To contribute, all that is necessary is say, take one or two "walking the Dom" type exercises or some other effect stuff, perhaps innerHTML, and put it up as a Task, say DOM Exercises and let people try them out.

  • Anónimo   13 de mayo de 2011 a las 05:27

    Comment from Krabat (participant)

    I think the task of week2 was quite hard to accomplish and all the concepts of functions, objects, closures, this keyword and object inheritance are still new for us. Therefore maybe it would good if we could practise that a bit more. Personally, I think the practical/programming homework tasks were easy while the theoretical ones were really hard. @selfstudier, maybe you could provide the course with practical exercises regarding the above concepts? That would be really great and appreciated!

  • Anónimo   13 de mayo de 2011 a las 05:59
    En Respuesta A:   Anónimo   13 de mayo de 2011 a las 05:27

    @Krabat

     

    You say that the Homework part is easy,

    If it is easy, why have so many people not completed it? (or even Week1)?

    The best way for you to practice the theory is to help others understand it.

    I don't know how to answer your question because you have included nearly everything that is in Week 2.

    Why don't you start separate Tasks for each item? eg a Task for Functions, a Task for Closures etc?

    Then we can deal one by one.

     

  • Krabat   13 de mayo de 2011 a las 07:55
    En Respuesta A:   Anónimo   13 de mayo de 2011 a las 05:59

    I said (for me) the practical/programming part was easy, but not the theoretical part (and I did other programming before). In the exercise we practised how to pass a function as a parameter and error handling, but all the conceptual stuff like Closures, Scope, Class inheritence etc. was only reflected in theory.

    It seems to me that only a few completed week2 because of the lack of practise in contrast to the  complex theorical part. If that will be taken care of in the following weeks my post can be ignored. If not I hope by giving us more practical execises more people here will have a better understanding of JS.

    Regarding the tasks: Should I start creating a task here? I can do a simple one for functions...

    So how about the other participants? What do you think / like the course to be?

  • Adam Fraser   13 de mayo de 2011 a las 08:14
    En Respuesta A:   Krabat   13 de mayo de 2011 a las 07:55

    I found this weeks stuff really difficult it might just be because I haven't done any programming before this course but with a bit of research and looking at how other people answered the questions I think I kind of got my head around it. 

    I agree with the Krabat though, I find doing actual javascripting tasks much more fun and I feel like I'm learning more.  

    Is there a site somewhere that has a bunch of JS tasks or something like that? I've had a look but couldn't really find much.  

    For example I really want to practice my JS but don't really have a project in mind that I could work on so I guess what I'm asking is whats the best way to practice actual JS rather than theory?? 

    Love the course by the way.

  • Anónimo   13 de mayo de 2011 a las 08:25
    En Respuesta A:   Krabat   13 de mayo de 2011 a las 07:55

    @Krabat

    Participants can create a Task by clicking the "New Task" button on the Front page.

    Then you give your Task a title (for example, "5 ways to call a function" ).

    Then you make some preliminary comments about why you created the Task:-

    (for example, the Week 2 material explains what a function is but doesn't give many examples of functions being called in practice, this task is to show different examples of that).

    Above is only examples, you can make any Task or Tasks that you like.

  • Anónimo   13 de mayo de 2011 a las 08:33
    En Respuesta A:   Adam Fraser   13 de mayo de 2011 a las 08:14

    @Adam

    Part of the problem is to give meaningful code examples that you and others already know how to understand.

    If someone just finished Week 1, starting from the beginning, the code examples need to be very simple (OK, it is true that some people will find it too simple but others may think they are hard, it depends what they know already, some people tried to do the course without knowing anything about programming or Javascript, only basic Html markup and some CSS).

    I have lots of code examples that I can contribute to different Tasks.

    Put up the Tasks and I will see what I can put up.

  • Anónimo   14 de mayo de 2011 a las 08:25
    En Respuesta A:   Adam Fraser   13 de mayo de 2011 a las 08:14

    @Adam

    Can you explain what you mean by "actual javascript tasks"?

    I posted a code example below for calculating a point distance from Origin (could be used eg in drawing objects on screen).

    Is that the sort of thing that you mean? And do you mean that these tasks should be set as aassignments or do you mean that  the code should just be presented for you to work through and try to understand?

    I'm asking because you say you looked on the internet and there wasn't much about but there are plenty of code examples on the internet (even if many of them are wrong/out of date).