-3

So basically I'm making my own Javascript framework/library. It has gone pretty good but I'm stuck on making my custom way of adding event listener.

Here's my code:

function $Listen(to,type,action){
    to.addEventListener(type{action});
}

My expected result was such that you could now do something like this:

var tag=document.getElementById("tag")
$Listen(to=tag, type="click",action=function(){ 
 alert("Hello")
});

Basically meaning when you click "tag" it alerts hello world, but the problem is, this script doesn't work. Is there anyway to make this code work? Also is there any way to make it easier to use my eventListener. for example:

    tag.$Listen("click",function(){
   //code goes here
    })

If you can that will be great, but just so you don't confuse me too much, please first focus on fixing my error, and if you have time, then you can find a way to make it easier. Also, I enjoy the more lower lines code, or simpler owners, just keep that in mind. THANKS!

Michal
  • 2,078
  • 23
  • 36
  • 1
    `(type{action})` is invalid syntax... also, default parameters (if that's what you were intending) should be listed in the function definition, not an invocation... – CertainPerformance Oct 24 '18 at 23:02
  • @CertainPerformance oh,ill update my question then. But just saying,i know there is a way to adjust my code and make it work.Hmm,maybe more help will come on later in the day –  Oct 24 '18 at 23:05

1 Answers1

1

You can add that function to Element.prototype as follow:

Prototype Inheritance. All JavaScript objects inherit properties and methods from a prototype. ... Person objects inherit from Person.prototype. The Object.prototype is on the top of the prototype inheritance chain: Date objects, Array objects, and Person objects inherit from Object.prototype.

When a function is created in JavaScript, JavaScript engine adds a prototype property to the function. This prototype property is an object (called as prototype object) has a constructor property by default. constructor property points back to the function on which prototype object is a property.

Actually, I don't know what you need to do that. But here I go:

Element.prototype.$Listen = function(type, action) {
  this.addEventListener(type, action);
}

var tag = document.getElementById("tag");
tag.$Listen("click", function() {
  alert(this.name)
});
<button id='tag' name='EleFromStack'>Click me!</button>

Look at this question to understand a little about Prototype

Ele
  • 33,468
  • 7
  • 37
  • 75
  • Can you edit your question to explain the script? Like saying what element prototype is? Im not like one of those masters when it comes to javascript –  Oct 24 '18 at 23:10
  • If you explain the script ill mark your awner as correct,im just trying to learn,and its hard when you dont tell me what the stuff means,like Element.Prototype –  Oct 24 '18 at 23:13
  • 1
    @DanielPrince wait, I'm looking a good already asked question in SO. – Ele Oct 24 '18 at 23:14
  • @DanielPrince I added a link which points to an asked question about prototype in JS. – Ele Oct 24 '18 at 23:18
  • @DanielPrince Pro tip: you'll need to learn JavaScript to write a JavaScript framework. And you'll need to figure out how to figure stuff out. – Dave Newton Oct 24 '18 at 23:19
  • @Dave Newton I already know javascript! Its just some things i dont understand. Not every developer knows everything. –  Oct 24 '18 at 23:23
  • anyways,thanks for the awnser! DusionDigital can now be continued! –  Oct 24 '18 at 23:25
  • @DanielPrince You may know *some* JavaScript, but if you're unaware what prototypes are, or how to find out, that's a *really* big gap. It's how JavaScript works. Obviously nobody knows everything (duh?) but IMO this is kind of mandatory for "knowing JavaScript". YMMV. – Dave Newton Oct 24 '18 at 23:28
  • @Dave Newton Achully,I know alot of javascript. I once created my own game called TerenStudios. I even made some cash from designing apps with Node.Js. Just because i might not know some things like prototypes,dosent mean I dont know javascript,better yet,you dont have the right to tell me what i need to make a javascript framework,because even if i dont know what prototypes are,if i have the power to make games and powerful applications with javascript,then i can make a javascript framework.DusionDigital is getting good,even if you are trying to discourage me,its not working –  Oct 24 '18 at 23:34
  • I'm going to delete this answer hehehehe – Ele Oct 24 '18 at 23:35
  • @DanielPrince I have no interest in encouraging/discouraging you. I have the right to say I believe not knowing prototypes, or how to find out what they are, means you don't "know JavaScript", just like you have the right to disagree. That said: any reasonable JS interview will include a discussion on JS OOP. If the interviewee doesn't have any idea, they don't "know JavaScript". If they didn't even know how to find out, not only do they not know JavaScript, they don't know about MDN, or how to search the web. That's a non-starter and the interview is over. Again, YMMV. – Dave Newton Oct 24 '18 at 23:42
  • im an online learner probally explains why i didnt know prototypes.But i know pretty much everything else in javascript,and i just learned prototypes..soo,look at the one whos talking(tsk tsk i know prototypes),when my framework reaches the top of google we will see who "Dosent Know Javascript",**your words dont hurt me**,your trying to make me mad,and quit my project,its not working –  Oct 24 '18 at 23:48
  • @Ele,delete the awnser please! I dont want anyone knowing about my language yet,nor my source code –  Oct 25 '18 at 00:14