JavaScript JavaScript Namespaces + Setter

Well, I'm using object orientation in JavaScript, but instead of using new, I just call the method from the namespace. In the moment, I have the following code:

var Component = {
    Button: function(_text, use_image) {
        button = $.createElement('button')
        if (use_image != false)
        {
            button.innerHTML = _text;
        }
        else
        {
            button.innerHTML = _text
        }
        var text = _text
        return button
    }
}

When I want to return a button, I do:

x = Component.Button("Click me")

And after I can use x. But if I want to change the text of x, I must use x.textContent. I'd like to instantiate this and use a setter to apply its text, this way:

x = new Component.Button("Click me")
x.text = "Don't click me"
document.getElementsByTagName("body")[0].appendChild(x)

And if I try to apply the setter text, it becomes global, I want to have a unique for each button. Mix namespaces with get/set.

Thank you in advance

Answer:1

One way can be like, I am not sure if this is the best approach. Looking for more answers

var Component = {
    Button: function(_text, use_image) {
        var button = {
            node : document.createElement('button'),
            setText : function(txt){this.node.innerHTML = txt;}
        };        
        if(_text){button.setText(_text);}
        return button;
    }
};

x = Component.Button("Click me");
x.setText("Don't click me");
document.getElementsByTagName("body")[0].appendChild(x.node);

y = Component.Button("Click me2");
y.setText("Don't click me2");
document.getElementsByTagName("body")[0].appendChild(y.node);

http://jsfiddle.net/2LCyn/

Answer:2

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode ...

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode ...

  1. reverse order of characters in excel
  2. reverse order of characters
  3. reverse order of characters in a string java
  4. reverse order of characters in c
  5. excel reverse order of characters in a cell
  6. reverse the order of characters in a string c++

I was looking after region based code folding for javascript files in eclipse. The only thing I could find so far was Coffee Bytes Code Folding which extends the editor folding settings in eclipse....

I was looking after region based code folding for javascript files in eclipse. The only thing I could find so far was Coffee Bytes Code Folding which extends the editor folding settings in eclipse....

  1. javascript code folding
  2. vscode javascript code folding
  3. eclipse javascript code folding
  4. eclipse javascript code folding not working
  5. visual studio code javascript folding

With a setup like; var o = $(' <div class="a a-1"> <div class="b"></div> <div class="c"> <div class="a a-2"></div> ...

With a setup like; var o = $(' <div class="a a-1"> <div class="b"></div> <div class="c"> <div class="a a-2"></div> ...

  1. find_element_by_class
  2. find class elements jquery
  3. find_elements_by_class_name
  4. find elements by class name selenium python
  5. find elements by class name selenium
  6. find_elements_by_class_name python
  7. find elements by class selenium python
  8. find elements by class selenium
  9. find_elements_by_class_name example
  10. find elements by class name selenium java
  11. find elements by class name javascript
  12. find elements by class name jquery
  13. find_elements_by_class_name text
  14. find_elements_by_class_name python webdriver
  15. find_elements_by_class_name click
  16. find elements by class javascript
  17. find elements by class python
  18. find elements by class name selenium c#
  19. find elements by class js
  20. find elements without class jquery

I need to do let the last reply scroll into view using jQuery. Here is my code: function showHideReplies(replies){ replies.each(function() { if($(this).index() < nIniRep || afterReply){ ...

I need to do let the last reply scroll into view using jQuery. Here is my code: function showHideReplies(replies){ replies.each(function() { if($(this).index() < nIniRep || afterReply){ ...

  1. scrollintoview in jquery