SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,117
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)

    unknown js syntax / features

    1. jQuery wraps it's entire code base in:
    Code:
    (function( window, undefined ) {
      
    })(window);
    Is this used so you can define any functions inside there without littering up the global namespace?
    Then explicitly expose parts you want with window.xxx = yyy; ?

    What is the code precisely doing?
    It looks as if you can pass any number of objects into it - I'm not sure what the (window) at the end is doing.

    --
    2. prototype keyword:
    is the prototype keyword's sole purpose to add functions to all instances of existing objects?
    Code:
    Car = function(make) {
    	this.make = make;
    }
    var honda = new Car('honda');
    Car.prototype.numberOfWheels = 4;
    alert(honda.numberOfWheels);
    Can't you just add all the properties to an object before you create instances of them? Why is it necessary to add this dynamically after initialisation?
    3. multiple = assignments:
    There's code like this used throughout:
    Code:
    jQuery.fn = jQuery.prototype = {}
    It seems pretty straight-forward but wanted to check I had understood correctly, all variables on the left of the last assignment get the lasts value.
    Code:
    var one = 1;
    var two = 2;
    var three = 3;
    var x = one = two = three;
    alert(one);
    alert(two);
    alert(three);
    alert(x);
    Thanks,

  2. #2
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All of those could have very easily been Googled, you know?

    Cheers,
    D.

  3. #3
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,117
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by disgracian View Post
    All of those could have very easily been Googled
    <sarcasm>Thanks for your help</sarcasm>

  4. #4
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what help you think you need.

    1. You're actually correct, but it's extremely easy to test that assumption anyway:
    Code:
    (function(arg) {
      var test = arg;
      alert(test); // prints 'robots'
    })('robots');
    
    alert(test); // prints nothing
    2. Google "javascript prototype".
    3. You weren't even asking a question.

    Cheers,
    D.

  5. #5
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,117
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    It's just different to any other syntax to what I have seen - they weren't stupid questions.

    The second set of brackets () call the function inside the first set.

    Thanks,

  6. #6
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not saying they're stupid questions; just that you were probably better off asking a search engine than a forum. It would have been a lot quicker than waiting for somebody to reply.

    Cheers,
    D.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •