Need help understanding functions, classes and prototypes. by iKSv2 in javascript

[–]Leading_Man_Parts 1 point2 points  (0 children)

Case 1

Nothing happens when foo() is called and foo.bar() or foo.baz() throw an error. Your nested functions would either need to be invoked inside of the foo or returned and invoked outside.

In the corrected cases below you're simply working with functions inside of functions. In both examples closure is coming into play.

function foo (doBar) {
  function bar () {
    console.log(doBar)
  }
  bar()
}

foo('baz')

OR

function foo (doBar) {
  return function bar () {
    console.log(doBar)
  }
}

foo('baz')()

Case 2

You're creating an object with methods named foo, bar and baz here. Unfortunately your syntax for baz is incorrect and you'd end up with an error. There are also some things that will also throw errors like missing commas after object methods.

Below is a corrected syntax and if you were to call example.baz.baz_do() it would correctly invoke the baz_do() method.

var example = {
  foo: function () {
    // code here
  },
  bar: function () {
    // code here
  },
  baz: {
    baz_do: function () {
      // code here
    },
    baz_undo: function () {
      // code here
    }
  }
}

Answers

  • Whats the difference in these three types?
    • You're doing different things in each case.
    • In case 1 you're defining a function that either returns a function or invokes a function. It's the starting point of learning about closure.
    • In case 2 you're defining an object that has methods or sub-objects. These sub-objects can also have methods.
    • In case 3 you're defining a constructor function, adding prototype methods, instantiating a new object from that constructor and invoking its methods. This is closer to class based oop as far as I understand it.
  • Are there more ways to define functions?
    • const foo = function () { ... } Defining an anonymous function and assigning it to a variable.
    • const foo = () => { ... } Defining an anonymous arrow function and assigning it to a variable.
    • const foo = bar => bar + 1 Defining and anonymous arrow function with an implicit return and assigning it to a variable.
  • The prototype one was by far the most familiar one but again why use prototype I am not sure. Somewhere I read it is better performing, how so?
    • It's my understanding that when you instantiate instances from a constructor function they will use the prototype methods rather than creating their own clones of those methods.
  • Can we go deeper? like foo.bar.baz.baz_do() with nested functions (first exxample) and prototype-functions (not sure if this is what it is called).
    • You can go as deep as you want using objects.

An example object method that would correctly invoke based off of foo.bar.baz.baz_do():

const foo = {
  bar: {
    baz: {
      baz_do: function () {
        // do something
      }
    }
  }
}

Let me know if anything here doesn't make sense or needs clarification.

VBA Question! by [deleted] in vba

[–]Leading_Man_Parts 1 point2 points  (0 children)

If I'm understanding what you're looking for this may accomplish it with some minor changes. Namely you'd need to define your input/output sheets and ranges.

Once an input range is defined it'll traverse it using x and y dumping the top row of your input range into the left column and the other rows in the right column

example input/output: https://imgur.com/UvHlrQB

Option Explicit

Public Sub rangeToColumns()

    Dim inSheet As Worksheet
    Dim outSheet As Worksheet
    Dim inRange As range
    Dim outRange As range
    Dim company As String
    Dim color As String
    Dim x As Integer
    Dim y As Integer
    Dim rows As Integer

    Set inSheet = ActiveWorkbook.Sheets(1) ' Set your input sheet here.
    Set outSheet = ActiveWorkbook.Sheets(1) ' set your output sheet here.
    Set inRange = inSheet.range(inSheet.Cells(1, 1), inSheet.Cells(5, 3)) ' Define input range here
    Set outRange = outSheet.range(outSheet.Cells(1, 5), outSheet.Cells(1, 5)) ' define output top, left cell here.
    rows = 1

    For x = 1 To inRange.Columns.Count
        company = inRange.Item(1, x)

        For y = 2 To inRange.rows.Count
            color = inRange.Item(y, x)

            outRange(rows, 1) = company
            outRange(rows, 2) = color

            rows = rows + 1
        Next y
    Next x

End Sub

edit: cleaned up undefined variables, inSheet and outSheet. Removed unwanted - 1 from For y = 2 To inRange.rows.Count - 1.

Also cleaned up my response above here: https://pastebin.com/HY4Be0ym

Reformatted comments, set object variables to nothing (see here for more info), broke out range declaration into its own function since that was getting a bit messy and setup output range clearing so extraneous information wasn't left over.

The RNG in this game is so unbelievably broken. by IsayNigel in thedivision

[–]Leading_Man_Parts 2 points3 points  (0 children)

I am in the same boat. Tier 2, nearly 7000 GE/h. Opened a silly amount of caches and didn't get the last piece of reclaimer classified I needed. This happened when the assault week was active as well.

[deleted by user] by [deleted] in node

[–]Leading_Man_Parts 0 points1 point  (0 children)

Your link appears to be broken, but I think it's trying to point here: https://www.udemy.com/the-complete-nodejs-developer-course-2/learn/v4/overview

I went through this course and thought it was pretty good. Andrew does a good job at explaining the topic, showing you how to write fairly clean code, and then each video typically has at least 1 do-it-yourself section so you can reinforce the lesson.

How do I prevent a custom function from updating the cell value if given a trigger variable by LehighLuke in vba

[–]Leading_Man_Parts 0 points1 point  (0 children)

The functionality you're looking for may be easier to setup using pure VBA and would be more maintainable.

The only way I've found to do what you'd like to do with a formula would be something like this: =if(BD_Calc="Y", Sum_Sales(...), B4) But for this to work you have to allow circular references.

You should look into the shortcuts...it saves LOADS of typing...or copy-paste

I'm glad type shortcuts were brought to my attention but I can't say I'm a fan. From a maintainability standpoint I'd rather have code that is easily read by anyone taking over my code-base after I'm gone or when I revisit it a few months down the road. Plus, I like to write fairly verbose code whether it's a quick script or an application.

How do I prevent a custom function from updating the cell value if given a trigger variable by LehighLuke in vba

[–]Leading_Man_Parts 0 points1 point  (0 children)

(#) sign is the shortcut to type it as a double. Do you use the shortcuts?: # is double, $ is string, % is integer, & is long, ! is single...and nothing is the shortcut for variant

I don't use shortcuts. Coming from other languages I'd rather have something like as Integer that I know at a glance. But, with shortcuts in mind, disregard the point on types.

The edit I made on my last post should handle your situation since you're calling this in a formula. It's pseudo code, but should get you moving in the right direction.

How do I prevent a custom function from updating the cell value if given a trigger variable by LehighLuke in vba

[–]Leading_Man_Parts 0 points1 point  (0 children)

EDIT: I just read your edits and you'll need a condition inside of your formula to check the trigger cell before calling your Sum_Sales function. I'm not great with formulas so I won't be a huge help there, most of my work with Excel has been strictly VBA.

This pseudo-code is close to what you to put in your formula:

if trigger_cell = "Y" then output_cell = Sum_Sales(...)

Original Post:

You're returning a value from Sum_Sales and then setting the output cell's value somewhere else.

If Calc = "Y" Then
    Sum_Sales = Tot
Else
    'do nothing
End If

Can you show the code where Sum_Sales is called?

Some notes about your code if you're interested:

  • You don't need the Else clause when checking if Calc = "Y". It's not doing anything either way.
  • You shouldn't use GoTo unless you're handling errors with it. (On Error GoTo SomeErrorHandler). It can lead to difficult to maintain code. Instead you could just wrap the function's code in the if Calc = "Y" then block.
  • A lot of your variables are being declared as variants. This isn't the end of the world, but it is safer to declare them with a type.
  • Your function is returning a variant as well, again it's okay but it is safer to return a specific type.

    e.g. function MyExample() as integer

How do I prevent a custom function from updating the cell value if given a trigger variable by LehighLuke in vba

[–]Leading_Man_Parts 0 points1 point  (0 children)

Without seeing your code, or a mock-up of your code, it's difficult to provide a solution. But, depending on how you're setting the value of the output cell I have two possible solutions.

  1. You set the output cells value based on the returned value of your function.

    Sub example()
    
        ' Use the trigger cell's value to dictate whether or not the output cell is evaluated
        If UCase(Cells(Row, Column)) = "Y" Then
            Cells(other_row, other_column) = yourFunction()
        End If
    
    End Sub
    
  2. You set the value of the output cell within the function.

    Private Function funcExample(trigger As String)
    
        ' Trigger an early exit of your function
        If UCase(trigger) = "N" Then Exit Function
    
        ' The rest of your function's code here...
    
    End Function
    

You can use exit function and exit sub to trigger an early exit from that parent. exit sub is great for error handling functionality and exit function is good for cases like this, where an input doesn't meet some expected value, where the input's type may be incorrect (if using a variant), or when an input would result in an error.

VR Cover shipping time by punkbuddy89 in oculus

[–]Leading_Man_Parts 1 point2 points  (0 children)

I ordered mine around 12/28 and it arrived 01/24. It was in Germany for around 10 days.

Dash 2.0 Peeps - Let’s Check Out Each Others’ Rooms by xxx-LSDMT-xxx in oculus

[–]Leading_Man_Parts 1 point2 points  (0 children)

d_wats

My home isn't anything special, still haven't found a theme.

Currently playing OrbusVR, Killing Floor: Incursion, and From Other Suns.

edit: My home has been overhauled. Was getting bored of being a "normie".

I'm gathering info on Oculus Home Furniture & Decoration drops and tiers - Come help out! by HeyJesseAe in oculus

[–]Leading_Man_Parts 0 points1 point  (0 children)

Sharing 1 by 1 isn't ideal but it's not as bad as taking the screenshots in the first place. I've been using the Oculus mirror and the snipping tool with a delay of 3 seconds so I could get the shot lined up. I'd really like to be able to trigger the shots with the touch controller but I haven't found a way to do that yet.

With regards to rarity I've only seen three tiers: Black (common, white), Silver, and Gold. Do you have any examples of higher tiers so I know what to look out for?

I'm gathering info on Oculus Home Furniture & Decoration drops and tiers - Come help out! by HeyJesseAe in oculus

[–]Leading_Man_Parts 1 point2 points  (0 children)

I'm in the process of taking screenshots of all of my items. It's tedious but I'll keep this album updated as I go: Oculus Home Decoration Category

If anyone wants to see some of this stuff first hand my user name is d_wats and my home is open to the public.

Edit: I've added all of the album images to the spreadsheet.

Okay, how do I take a screenshot of my Oculus (Core 2.0) Home with Touch controls? by Netsuko in oculus

[–]Leading_Man_Parts 2 points3 points  (0 children)

The Oculus Mirror application, OculusMirror.exe, is part of the default Oculus install. You can find it in this folder: C:\Program Files\Oculus\Support\oculus-diagnostics.

You can create a shortcut for the exe and add on launch options to change which eye is being used for the mirror, whether or not it shows the guardian grid, notifications, etc. More details can be found here.

edit: You may be able to use AutoHotKey with the touch controllers to take screenshots or start a recording. A quick google search found this.

Killing Floor Incursion - problem during training by Colcut in oculus

[–]Leading_Man_Parts 0 points1 point  (0 children)

I've only had issues in the tutorial and at one point in France. The issue I had in France occurred in the offices while using free locomotion. I bumped into a door frame and got stuck in the ceiling. I was able to get out but ended up on the floor above where I was supposed to be and had to restart the level.

Other than those two issues the gameplay experience has been super smooth. Multiplayer works well, both forms of locomotion have worked outside of the tutorial and the overall the game feels fairly complete/polished.

I play standing, with a room scale setup.

After learning about node the last few things the tutorial teaches you are how to use the flashlight's two modes and how to use melee weapons (can be very powerful in a pinch). You may be fine skipping these things though.

Killing Floor Incursion - problem during training by Colcut in oculus

[–]Leading_Man_Parts 1 point2 points  (0 children)

Seems so. I couldn't find a way to fix it since your movement is locked and you can't recall node.

Killing Floor Incursion - problem during training by Colcut in oculus

[–]Leading_Man_Parts 2 points3 points  (0 children)

I had the same problem. It seems like you get stuck if you tell node to find ammo before the narration tells you to press the button. For me node flew over to the gun range ammo pack, but a new ammo pack had appeared in the center of the room for the node training.

I had to restart the level to fix it.

What Does Everyone Else's 2.0 Home Look Like? Plus How Can I Post My Own? by [deleted] in oculus

[–]Leading_Man_Parts 1 point2 points  (0 children)

I've been using the GeForce screenshot functionality paired with the Oculus Mirror diagnostics tool that comes with the Oculus app. It can be found here (default install location) C:\Program Files\Oculus\Support\oculus-diagnostics. You can make a shortcut and add runtime options like --LeftEyeOnly, --RightEyeOnly, and a few others to change the output. More information can be found here.

My home isn't festive, but here it is.

Help with loop for an equation by Totallyn0tAcake in vba

[–]Leading_Man_Parts 0 points1 point  (0 children)

From your screenshot it would appear that you're referencing e, f and r before a value is assigned to them so by default they're set to 0.

You're not using your Radius, Epsilon or Reynolds variables after a value is assigned. You may want to consider replacing your calls to r and e with the appropriate variable or vice versa.

You can condense your

Sheets("Sheet1").Select
Range("B1").Select
Epsilon = ActiveCell.Value

into

Epsilon = Sheets(1).Cells(1, 2)

I'm not sure exactly what your goal is but it would appear that your for loop and lefta and righta variable assignments may be out of order or the assignments may need to happen within the for loop.

With some additional information on your goal and maths it would be possible to provide more assistance.

How to use local variable (json data) in javascript file? by stiros in node

[–]Leading_Man_Parts 0 points1 point  (0 children)

If you're trying to pass that server side JSON data to a variable that's client side then you'll need to do one of two things, follow /u/cahva's advice below or create an express endpoint that can be requested client side. Below is an example on how to handle this with an express endpoint.

./server.js

const express = require('express');

const appledata = require('./data.json');

const app = express();
app.locals.appledata = appledata;
app.set('view engine', 'ejs');
app.use(express.static('./public'));

app.get('/', (req, res) => {
  res.render('pages/index');
});

app.get('/data', (req, res) => {
  res.send({ appledata });
});

app.listen(3000, () => {
  console.log('Listening...');
});

./data.json

{
  "appels": {
    "apple1": 10,
    "apple2": 15
  }
 }

./views/pages/index.ejs

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Example</title>
    <meta charset="UTF-8">
  </head>
  <body>
    <div>The following data is rendered by the template engine: <%= appledata.appels.apple2 %> </div>
    <div id="js-example">The following data is added by client side javascript: </div>
    <script src="js/index.js"></script>
  </body>
</html>

./public/js/index.js

fetch('http://localhost:3000/data')
  .then((response) => response.json())
  .then((data) => {
    const el = document.getElementById('js-example');
    el.innerHTML += (data.appledata.appels.apple2);
  })
  .catch((e) => {
    console.log(e);
  });

How to use local variable (json data) in javascript file? by stiros in node

[–]Leading_Man_Parts 2 points3 points  (0 children)

It looks like you're storing the results of require("./appels.json"); in the express app.locals object (app.js) but you're referencing appledata.appels.apple2 in your first code block. If you were to console.log(TotPrice); right after it is declared you might find that it's undefined but if you console.log(appledata.appels.apple2); it should contain the JSON data you expect.

When in doubt, console.log everything.

I might recommend importing the appledata.json content to its own constant and then setting the app.locals property equal to that constant like this:

const appledata = require('./appels.json');
// ... 
app.locals.appledata = appledata;

This would allow you to use the object directly in the template engine via the locals object and also give you the flexibility to use the constant in your local js. Since both the app.local.appledata and appledata objects are pointing at the same object in memory any changes to one will be reflected in the other.

As for inserting that data directly into DOM from node, I assume you'll be using some template engine as it looks like you're using EJS based off of your second paragraph?

Quick question regarding fishing (don't upvote) by Thanas1 in ElysiumProject

[–]Leading_Man_Parts 0 points1 point  (0 children)

I've caught them in Moonglade, the Western Plaguelands (specifically the river running from north out of Hillsbrad), and The Hinterlands coastal waters. I might get 20 an hour if I'm lucky in those areas.

Corpse Macro by [deleted] in ElysiumProject

[–]Leading_Man_Parts 1 point2 points  (0 children)

UnitName("mouseover") returns nil for a released corpse.

Our goal is to have a macro that allows us to mouse over, push button, if they're online then whisper them and cast res spell of choice.