You do not need to compare with thisat all, since you want it to work with what you are switching to $. Just leave if:
function $(selector) {
var resultObject = {
hide: function () {
selector.style.visibility = "hidden";
}
};
return resultObject;
}
<input type="button" value="hide myself with my jquery " onclick="$(this).hide();" />
Run codeHide resultNow, if your selector argument may be something other than a DOM element, but a CSS selector, you need to check the argument for its data type. Keep in mind also that CSS selectors can represent more than one element, so if you want to deal with this, you'll need a loop.
, :
function $(selector) {
var nodes = typeof selector === "string"
? Array.from(document.querySelectorAll(selector))
: [selector];
var resultObject = {
hide: function () {
nodes.forEach(function (node) {
node.style.visibility = "hidden";
});
}
};
return resultObject;
}
<input type="button" value="hide all divs with my jquery "
onclick="$('div').hide();" />
<div>div 1</div>
<div>div 2</div>
Hide result, , , resultObject :
function $(selector) {
var resultObject = {
nodes: typeof selector === "string"
? Array.from(document.querySelectorAll(selector))
: [selector],
hide: function () {
resultObject.nodes.forEach(function (node) {
node.style.visibility = "hidden";
});
return resultObject;
},
text: function (txt) {
resultObject.nodes.forEach(function (node) {
node.textContent = txt;
});
return resultObject;
},
color: function (colorCode) {
resultObject.nodes.forEach(function (node) {
node.style.color = colorCode;
});
return resultObject;
}
};
return resultObject;
}
<input type="button" value="color and put text in divs with my jquery "
onclick="$('div').color('red').text('clicked!');" />
<div>div 1</div>
<div>div 2</div>
Hide result, resultObject $. , . , node (s),... ..
. , .; -)