Originally posted by wonderboy
View Post
- 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!
Technical interviews
Collapse
X
-
Knock first as I might be balancing my chakras. -
Originally posted by suityou01 View PostAlso line 2 starts with an opening parentheses. How can a statement begin with an opening parenthesis?
I need a lie down.Comment
-
Originally posted by wonderboy View PostThat 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).
Let me look at the original problem now you have demystified the voodoo for meKnock first as I might be balancing my chakras.Comment
-
Originally posted by wonderboy View PostHappy 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!
Ta.Knock first as I might be balancing my chakras.Comment
-
Originally posted by suityou01 View PostGot it. Ok thanks. All objects are reference types and are held on the heap. Moving onto line 2?Comment
-
Originally posted by suityou01 View PostOk 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.
Comment
-
Javascript is weird.
So to put it in a less insane way:
Code:var appVar1 = {}; function fn(appVar2){ appVar2.x = 10; } fn(appVar1)
Will work inside IR35. Or for food.Comment
-
Originally posted by suityou01 View PostOk not seeing how appVar1 and appVar2 are in any way related? Can we clear this up before we deal with scope?
Ta.
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.Knock first as I might be balancing my chakras.Comment
-
Originally posted by VectraMan View PostJavascript is weird.
So to put it in a less insane way:
Code:var appVar1 = {}; function fn(appVar2){ appVar2.x = 10; } fn(appVar1)
Comment
-
Originally posted by VectraMan View PostJavascript is weird.
So to put it in a less insane way:
Code:var appVar1 = {}; function fn(appVar2){ appVar2.x = 10; } fn(appVar1)
Yes this is exactly it as I understand it.Knock first as I might be balancing my chakras.Comment
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- IR35: Mutuality Of Obligations — updated for 2025/26 Today 05:22
- Only proactive IT contractors can survive recruitment firm closures Yesterday 07:32
- How should a creditors’ meeting ideally pan out for unpaid suppliers? Sep 19 07:16
- How should a creditors’ meeting ideally pan out for unpaid suppliers? Sep 18 21:16
- IR35: Substitution — updated for 2025/26 Sep 18 05:45
- Payment request to bust recruitment agency — free template Sep 16 21:04
- Why licensing umbrella companies must be key to 2027’s regulation Sep 16 13:55
- Top 5 Chapter 11 JSL myths contractors should know Sep 15 03:46
- Top 5 Chapter 11 JSL myths contractors should know Sep 14 15:46
- What the housing market needs at Autumn Budget 2025 Sep 10 20:58
Comment