javascriptmethodspure-js

I need an idea of creating methods that take elements before it


I need an idea of creating methods that take elements before it

note it should take a defined element instead of an object example:

"use strict";
var element = document.getElementById('name').value;
elment.capitalize();//the capitaze should be a function

i will appriciate for your help.


Solution

  • No you cannot use the method to get associated with a variable and invoke that. For that scenario we have objects to take care of. You can create a object data structure with necessary properties and functions so that when you call capitalize() function of that object you get the uppercase value of name property of that object.

    var element = {
     name: 'name',
     capitalize: function(){
       this.name = this.name.toUpperCase();
       return this.name;
     }
    }
    
    var name = element.capitalize();
    console.log(name);