• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "Technical interviews"

Collapse

  • TestMangler
    replied
    Originally posted by barrydidit View Post
    Now you can add JS to BS on your CV
    Fairly sure he had BS on his cv already.....all versions, many years experience

    Leave a comment:


  • barrydidit
    replied
    Originally posted by suityou01 View Post
    Makes sense to me, as much as slippety-sloppety loosely typed, declare anywhere voodoo code ever will.
    Now you can add JS to BS on your CV

    Leave a comment:


  • Bunk
    replied
    Originally posted by suityou01 View Post
    Nice post Bunk. Thanks.

    I think calling a method a constructor was confusing me as constructors are special. This is agin why I think the question could be better.

    Makes sense to me, as much as slippety-sloppety loosely typed, declare anywhere voodoo code ever will.
    Not in Javascript

    Code:
    function myFunction() {
      console.log('in the function');
      this.something = 'Hello';
    }
    
    myFunction();
    // Call the function
    // outputs 'in the function'
    
    var myObj = new myFunction();
    // Creates an object using the function as constructor
    
    console.log(myObj.something);
    // outputs 'Hello'

    Leave a comment:


  • wonderboy
    replied
    Originally posted by suityou01 View Post
    I think calling a method a constructor was confusing me as constructors are special. This is agin why I think the question could be better.
    I believe they named the property "MyConstructor" for this very reason.

    Leave a comment:


  • suityou01
    replied
    Originally posted by Bunk View Post
    I've spaced it out to explain each part.

    Code:
    var appVar1 = {};
    Creates a new global variable called appVar1 and initialises it as an object literal.


    Code:
    (
    We'll come back to this bit.

    Code:
    function(appVar2){
    
      appVar2.MyConstructor = function() {
        //...
      };
    }
    Create an anonymous function which takes a parameter appVar2 and adds a function MyConstructor to it.

    Code:
    )
    
    (appVar1);
    Now we close that opening bracket that I said we'd come back to. This makes the block of code a function expression instead of a function declaration. This is then immediately invoked by the following (appVar1); which passes the object literal appVar1 to the function. This all has the result of adding MyConstructor to appVar1.
    Nice post Bunk. Thanks.

    I think calling a method a constructor was confusing me as constructors are special. This is agin why I think the question could be better.

    Makes sense to me, as much as slippety-sloppety loosely typed, declare anywhere voodoo code ever will.

    Leave a comment:


  • Bunk
    replied
    Originally posted by suityou01 View Post
    Ok not seeing how appVar1 and appVar2 are in any way related? Can we clear this up before we deal with scope?

    Ta.
    I've spaced it out to explain each part.

    Code:
    var appVar1 = {};
    Creates a new global variable called appVar1 and initialises it as an object literal.


    Code:
    (
    We'll come back to this bit.

    Code:
    function(appVar2){
    
      appVar2.MyConstructor = function() {
        //...
      };
    }
    Create an anonymous function which takes a parameter appVar2 and adds a function MyConstructor to it.

    Code:
    )
    
    (appVar1);
    Now we close that opening bracket that I said we'd come back to. This makes the block of code a function expression instead of a function declaration. This is then immediately invoked by the following (appVar1); which passes the object literal appVar1 to the function. This all has the result of adding MyConstructor to appVar1.

    Leave a comment:


  • wonderboy
    replied
    Originally posted by suityou01 View Post
    Wait, I see it. () is your voodoo on the fly, shot in the arm for unreadable code, function declaration. Appvar2 is the declaration. Appvar1 is the ref, so in theory this is being injected, and the reference holds outside the function declaration.

    The constructor function pointer is also set inside the first function declaration.

    tulipty question really. To emphasise the manner of object references, there should really be some code after all this performs an operation on appvar1 that was defined on appvar2.

    IMVHO.

    I think the question is fine. It checks core understanding of basic concepts. That is if the interviewer himself does too...

    Leave a comment:


  • suityou01
    replied
    Originally posted by VectraMan View Post
    Javascript is weird.

    So to put it in a less insane way:

    Code:
    var appVar1 = {};
    
    function fn(appVar2){
    appVar2.x = 10;
    }
    fn(appVar1)
    And he doesn't understand that x is now part of appVar1?
    Yes JS is weird.
    Yes this is exactly it as I understand it.

    Leave a comment:


  • wonderboy
    replied
    Originally posted by VectraMan View Post
    Javascript is weird.

    So to put it in a less insane way:

    Code:
    var appVar1 = {};
    
    function fn(appVar2){
    appVar2.x = 10;
    }
    fn(appVar1)
    And he doesn't understand that x is now part of appVar1?
    Yes. And this was the worlds largest IBank, and they rejected me. But like an earlier poster said, after the interview I didn't want to work there anyway

    Leave a comment:


  • suityou01
    replied
    Originally posted by suityou01 View Post
    Ok not seeing how appVar1 and appVar2 are in any way related? Can we clear this up before we deal with scope?

    Ta.
    Wait, I see it. () is your voodoo on the fly, shot in the arm for unreadable code, function declaration. Appvar2 is the declaration. Appvar1 is the ref, so in theory this is being injected, and the reference holds outside the function declaration.

    The constructor function pointer is also set inside the first function declaration.

    tulipty question really. To emphasise the manner of object references, there should really be some code after all this performs an operation on appvar1 that was defined on appvar2.

    IMVHO.

    Leave a comment:


  • VectraMan
    replied
    Javascript is weird.

    So to put it in a less insane way:

    Code:
    var appVar1 = {};
    
    function fn(appVar2){
    appVar2.x = 10;
    }
    fn(appVar1)
    And he doesn't understand that x is now part of appVar1?

    Leave a comment:


  • wonderboy
    replied
    Originally posted by suityou01 View Post
    Ok not seeing how appVar1 and appVar2 are in any way related? Can we clear this up before we deal with scope?

    Ta.
    Code:
    var appVar1 = {}; //declare object literal (equiv. to "var appVar = new Object();")
    
    //wrap an anonymous function in parens to make it an expression, and then invoke immediately on the final line, passing in appVar1 (visible inside the anonymous function as appVar2)
    (function(appVar2){
    
    //create a property named MyConstructor on appVar2 (which is a reference to the same object as appVar1), and assign an anonymous function to it
    appVar2.MyConstructor = function() {
    //...
    };
    
    })(appVar1);
    
    //object "appVar1" now has a new property on it called MyConstructor pointing to the function defined above with the ellipsis inside.

    Leave a comment:


  • wonderboy
    replied
    Originally posted by suityou01 View Post
    Got it. Ok thanks. All objects are reference types and are held on the heap. Moving onto line 2?
    All Objects are reference types but primitives (String, Number, Boolean, null, undefined) are treated as value types (although there are corresponding wrapper Objects for String, Number and Boolean). Placement of strings on stack/heap I suspect involves some clever optimisation (I haven't bothered to ever find out). I wouldn't be surprised if Numbers and Boolean literals are put on the stack directly.

    Leave a comment:


  • suityou01
    replied
    Originally posted by wonderboy View Post
    Happy to do so. Given the following code (JavaScript), the interviewer claimed that "appVar2" needed to be returned from the function expression (using a "return" statement) to make "MyConstructor" available outside the scope of the closure created by the function expression. He claimed that by injecting appVar1 into the function expression it was being copied, implying that appVar2 referred to a separate object, hence the need to return.

    This is a fundamental misunderstanding of the way JavaScript uses "pass reference by value" (described more commonly as "pass by reference") when dealing with objects.
    Code:
    var appVar1 = {};
    
    (function(appVar2){
    
    appVar2.MyConstructor = function() {
    //...
    };
    
    })(appVar1);

    Mock me all you want!
    Ok not seeing how appVar1 and appVar2 are in any way related? Can we clear this up before we deal with scope?

    Ta.

    Leave a comment:


  • suityou01
    replied
    Originally posted by wonderboy View Post
    That is another awesome feature of JavaScript whereby enclosing a function in parens turns it into a function expression, which can then be immediately invoked (turning it into a valid statement).
    Ok. Seems horribly backwards. Perhaps I need to understand how not typing things and having undeclared functions is a good thing.

    Let me look at the original problem now you have demystified the voodoo for me

    Leave a comment:

Working...
X