7

We have a lot of instanceof in our Javascript framework. In locker service, instanceof doesn't work for most HTML objects because it uses a SecureElement instead of the actual object (E.G. HTMLElement).

In the documentation, they talk about using typeof, but for complex objects it's just useless.

I've tried to use other methods but instanceof is really the way to go and nothing seems to be compatible with Locker Service.

I've checked everywhere and found out that Doug talked about JavaScript Proxy in Summer '17 and it would solve my issue.

In the meantime, are there any workarounds or a way to have a "pre-pre-release" of Summer '17?

1
  • Very well asked question. +1 from me! Maybe Doug will weigh in here.
    – Adrian Larson
    Dec 27, 2016 at 21:38

1 Answer 1

2

Unfortunately, there's no way to get a "pre-pre-release" of Summer '17 that uses Proxy.

As a temporary solution until the release, have you tried verifying a subset of properties/methods on an object to try to determine what it is? This thread has some interesting ideas. You can try something similar to Eugene's answer (maybe without the cloneNode check):

if(typeof node == "object" && "nodeType" in node &&
   node.nodeType === 1 && node.cloneNode) {
  // most probably this is a DOM node
}

And you can add nodeName checks to try to tell what element type it is. Of course, this isn't a perfect solution since it would pass for an object that contains all the checked for properties. You could add additional checks until you feel confident nothing would 'accidentally' pass.

7
  • I like the effort, but how could I implement that for every type of objects?, HTMLElement is only an example. Where could I get the confirmation that instanceof will work in Summer '17? On github, SecureElement seems to already have the proxy code. Dec 28, 2016 at 19:14
  • @Marc-AntoineVeilleux Yes you can download the latest Aura open source project and play with it yourself. You won't be able to use it on the actual Lightning platform though. I did a simple test of var div = document.createElement("div"); and div instanceof HTMLElement passes. Keep in mind that instanceof doesn't work across frame/window boundaries and LockerService does leverage an iframe to evaluate some code so it's hard to guarantee all use cases will work. Do you have something specific in mind you want to try? Dec 28, 2016 at 21:24
  • Hi @TrevorBliss! I've just saw on the release notes (resources.docs.salesforce.com/206/latest/en-us/sfdc/pdf/…) that "The restriction on using the instanceof operator in JavaScript code has been removed due to some framework refactoring.", but I am unable to use div instanceof HTMLElement, how can I make sure I have the lastest Locker Service / Lightning framework? Feb 20, 2017 at 13:38
  • 1
    Yes feel free to open a case and share the case number here for us to track. Thanks. Feb 21, 2017 at 22:17
  • 1
    Done, Case # 15789383 Feb 22, 2017 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.