How can I extend jquery-comments? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am using jquery-comments to allow for commenting on my website. This works fine, but I would like to make some changes to how it works.
However, instead of making my changes to jquery-comments directly in the jquery-comments.js, I would like to put them in a different file, and extend/modify the Comments object if possible with my own functions.
For example I would like to change what happens in the function called createCommentingFieldElement.
How can I do that?

This is how I solved it:
When I initialize jquery-comments I can extend it with a function like this:
$('#comments-container').comments({
getComments: function (success: any, error: any) {
var extensionMethods = {
doSomething: function () {
// this is my extended function.
// here you have the this object available.
}
}
};
$.extend(true, $('.jquery-comments').data('comments'), extensionMethods);
}
Your new jquery-comments extended function can now be called like this:
$('.jquery-comments').data('comments').doSomething();

Related

How to create object inside object in JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to create an object inside an object in JavaScript like this:
{
"test": {
{},
{}
}
}
In more detail, I want to create a new object if "test" doesn't exist in the object, and create a new object in the same location as the old one if "test" exists.
Please help if this is possible.
You mean, like:
theObject["test"] = {};
this?
It's just an assignment, mate!
You need to give a key in every object you create in JavaScript.
E.g.:
var obj = {
objInside: {}
}
you can read more about object literals.

How to show method declaration in NodeJs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
is there any way to show function declaration. e.g what it takes as parameters and what it returns in CLI? as like "tinker in laravel"
I searched over internet but found nothing.
I am not sure if I correctly understand your question. To find out what a function looks like, including its parameters (and all its code including what it returns), you can do a (slightly dirty) hack like this:
function a(b,c) {
return b + ", " + c;
}
console.log(a.toString());
Output:
'function b(a,b){return a+" " + b}'
Alternatively, if you are looking for documentation, for most NPM modules and note itself, there is solid API documentation. See https://nodejs.org/api/ for example.

Inherit nearly all from a first function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hello I'm trying to make DRY code (not repeat code) from the a secondFunction that inherits NEARLY all the content from a firstFunction.
This would be the example of what I want but it's not DRY:
function firstFunction(){
this.arrayObjectsToElastic = ["hello1", "hello2"]
this.anothervariable1= "anothervariable1"
this.anothervariable2= "anothervariable2"
this.targetVariableToRemove = "something"
return [this.arrayObjectsToElastic]
}
function secondFunction(){
this.arrayObjectsToElastic = ["hello1", "hello2"]
this.anothervariable1= "anothervariable1"
this.anothervariable2= "anothervariable2"
return [this.arrayObjectsToElastic]
}
Therefore, I don't want to "inherit" in the secondFunction the targetVariableToRemove from the firstFunction because if so it'll crash in some other processes I'm running.
Maybe you can do something like:
function secondFunction(){
return firstFunction().concat(["newContent"]);
}

How to use `this` like an object and get its variables/functions by a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've got an object.
function Obj()
{
}
Obj.prototype.doSomething = function(thing)
{
this["do" + thing]();
}
Obj.prototype.doAlert = function()
{
alert("Alert!");
}
var obj = new Obj();
obj.doSomething("Alert");
This is just a shortened down version of my object, and is a lot bigger.
What I would like to do is that if you pass in 'Alert' it will run this.doAlert(); and if I pass in 'Homework' it will run this.doHomework();
Obviously, in this case, it is stupid to do it like this, but my final project is going to be completely different.
It works fine with window like this:
window["do" + thing]();
but I don't want it to be a global function, but to be part of obj.
Does anyone have an idea how I'd go about doing this?
Thanks in advance!
It turns out that when you get the function through this['functionName'], this is not bound to it.
This means you cannot use this.foo inside any function called like above.
To fix this, I used the following code:
this["do" + thing].bind(this)();
instead of this["do" + thing]();
JSFiddle: https://jsfiddle.net/auk1f8ua/

Determine order functions are called in [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So I have a list of functions in JavaScript, each function has an associated button.
I want to know how you can tell in which order the buttons were pushed.
There's no native way for JavaScript to keep track of when a function was called, as this would have too much of a performance impact on the engine. You'll need to modify your code to keep track of this information internally. For example, you could use an array to log each call.
var log = []; // List of calls made in order
function one()
{
log.push('one'); // Log call
// ...
}
function two()
{
log.push('two'); // Log call
// ...
}
function three()
{
log.push('three'); // Log call
// ...
}
// Call in some order (which could be done by the user of course):
two();
one();
three();
You could of course easily reset your log as well:
log = []; // Reset

Categories

Resources