1

Using PhpStorm: My code is passing a Instantiated Class as a Parameter into a function. I know that className is a reference to a particular class. But is there anyway to let my code become aware of what ClassName is?

   $(document).on('validSelection', function(event,className){
      alert( className.sayHello() );
   });

This would really make coding this particular function a lot easier since I plan on using ClassName a lot. In php, you can force the recognition by do somethings like:

/* @var $variable ClassName */
4
  • Do you mean className is a string containing the name of a class? Commented Jan 29, 2013 at 19:50
  • no. className is the class being passed as a parameter. so in this situation... alert( className.helloWorld() ); would work. Commented Jan 29, 2013 at 19:52
  • Then... what do you mean, "become aware"? That's not really a sensible phrase to use in this context. Commented Jan 29, 2013 at 19:53
  • I should have been more clear. Was looking to have IDE "be aware" when I type className.s... sayHello() pops up in a fairly short list. Commented Jan 29, 2013 at 20:16

2 Answers 2

1

My code is passing a Instantiated Class as a Parameter into a function

No, it isn't. You just added another parameter to the function's list, but when it's called (by jQuery, when the event happens), it's not passed. If you have access to className when you bind the event handler, you can just take advantage of the closure:

var className = {
    sayHello : function() {
       return "hello";
    }
};
$(document).on('validSelection', function(event){
   alert(className.sayHello()); // works
});

On the other hand: I see you're using a custom event. So, if you have a call to .trigger anywhere else on your code, then you can pass anything as the second argument, and it will be passed to the event handler:

var className = {
    sayHello : function() {
       return "hello";
    }
};
$(document).trigger('validSelection', className);

// On a separate scope...

$(document).on('validSelection', function(event, className){
   alert(className.sayHello()); // works
});
Sign up to request clarification or add additional context in comments.

3 Comments

My question was more related to the IDE (PhpStorm) being smart enough to know that 'ClassName' included 'SayHello()' .. But I think this question is spurred by my discovery of trigger() because at least in this case, the trigger is defined not knowing all the areas of code that might reference it. which is incredibly useful. Thank you for all you help on this!
Oh sorry, my brain just skipped the references the the IDE on your question. I hope my answer can be still useful for somebody else.
yeah. I think it was a reality check to what I was doing and thinking about as well.
0

In JavaScript we don't have classes but use "prototypes" to act like we do. So to check the "classness" of an object you need to use one of the prototype methods.

If you've got a readily-referable instantiated copy of the className object hanging around you can use className.prototype.constructor and check if it isPrototypeOf that other object.

Alternatively, you can check if that className is an instanceOf some prototypical object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.