Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#17156 closed defect (invalid)

TypeError: this._scanAmd is not a function

Reported by: Randy Hudson Owned by: Randy Hudson
Priority: undecided Milestone: tbd
Component: Parser Version: 1.9.0
Keywords: Cc:
Blocked By: Blocking:

Description

When calling parser's parse function, I'm getting the error: TypeError?: this._scanAmd is not a function

The code is trying to reference "this", which is window. It should be referencing 'parser' instead.

Change History (13)

comment:1 Changed 6 years ago by Colin Snover

Owner: changed from bill to Randy Hudson
Status: newpending

Please provide a test case demonstrating this issue.

comment:2 Changed 6 years ago by ben hockey

fyi - the parse method is supposed to be run in the context of the parser ie parser.parse() rather than var parser = parser.parse; parse();

all the API examples are written this way - http://dojotoolkit.org/api/1.9/dojo/parser#parse

comment:3 Changed 6 years ago by Randy Hudson

Status: pendingnew

It's not a good idea for static functions to make assumptions about context. For comparison, look at dom-geometry.js

comment:4 Changed 6 years ago by ben hockey

Owner: changed from Randy Hudson to bill
Status: newassigned

consider the parser like a singleton - these are not necessarily static methods. you can look at add and remove in dijit/registry for comparison.

i'll hand this over to bill from here.

comment:5 in reply to:  3 Changed 6 years ago by Colin Snover

Replying to randyhudson:

It's not a good idea for static functions to make assumptions about context. For comparison, look at dom-geometry.js

Unless you want to delegate and override one of the internal methods to make your own that does something slightly different…

comment:6 Changed 6 years ago by bill

Owner: changed from bill to Randy Hudson
Status: assignedpending

The parser is using "this.xyz" throughout the code, and I agree it's unusual, since the parser is not in fact a singleton. FYI, it used to be a singleton, see [28641].

I didn't think of the delegate() thing though, about how using this.foo lets an app "extend" the parser. So that's a good argument to leave things as is.

So, I'm inclined to close this ticket since no test case was given, even though we asked for one. Last chance...

comment:7 Changed 6 years ago by Randy Hudson

Status: pendingnew

Example code could be anything from storing an alias to the function (like in comment 2), or something like:

…
on (someWidget, 'setMagicNode', parser.parse);
…

i.e., create a binding such that whenever setMagicNode(/*DomNode?*/node) is called on some widget, the new node is then parsed.

comment:8 Changed 6 years ago by Colin Snover

Resolution: invalid
Status: newclosed

Use lang.hitch; this is exactly what it is for.

on(foo, 'event', lang.hitch(bar, 'method'));

comment:9 Changed 6 years ago by Randy Hudson

I'm well aware of the workarounds, but since this is not (any longer) an instance method, it is unintuitive an unnecessary to use hitch.

comment:10 Changed 6 years ago by Randy Hudson

Why is this invalid? At a minimum, the API documentation should indicate that while:

   var padExtents = geom.getPadBorderExtents;
   var extents = padExtents(node, style);

works fine, the following does not:

   var parse = parser.parse;
   var widgets = parse(node);

Really, there's no reason for the code to use 'this' any more. 'this' and 'self' can be replaced with parser (also making the code smaller).

Last edited 6 years ago by Randy Hudson (previous) (diff)

comment:11 Changed 6 years ago by Colin Snover

Is it documented that the former will work? It runs counter to how JavaScript? works generally and limits what we can do to the API, so I’m not sure why we would want to explicitly document it.

comment:12 in reply to:  11 Changed 6 years ago by Randy Hudson

I guess that makes query.js wrong when it does this:

var … , forEach = array.forEach;

Functions are passed around as properties of objects all the time. That does not mean that you can only call those functions in the context of the referring object.

comment:13 Changed 6 years ago by bill

It's really case by case. You can't do that with dojo/router, for example, because it returns a singleton.

Note: See TracTickets for help on using tickets.