I'm using protractor with jasmine 1.3, tried adding a custom matcher to my spec using the example here
beforeEach(function () {
utils.log("beforeEach");
this.addMatchers({
toBeGoofy: function (expected) {
if (expected === undefined) {
expected = '';
}
var pass = this.actual.hyuk === "gawrsh" + expected;
if (pass) {
this.message = "Expected " + this.actual + " not to be quite so goofy";
} else {
this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy";
}
return pass;
},
});
});
note that I didn't change anything from their example.
after that, i try using it inside an "it" like that:
expect({ "hyuk": "j" }).toBeGoofy();
and i get an error:
TypeError: undefined is not a function
on the line that the matcher was used on..
any help?
The problem was the matcher definition apparently.
instead of:
if (pass) {
this.message = "Expected " + this.actual + " not to be quite so goofy";
} else {
this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy";
}
this message should be an array of 2 messages, first for pass, second for not.matcher so it would be something like this:
this.message = function() {
return [
"Expected " + this.actual.hyuk + " to be gawrsh",
"Expected " + this.actual.hyuk + " not to be gawrsh"
];
};
Related
getString(comment) {
const authorName = comment.getAuthor().getName();
if (!comment.getRepliedTo()) return authorName;
return `${comment.getMessage()} by ${authorName} (replied to ${this.getString(comment.getRepliedTo())})`;
}
toString() {
const authorName = this.getAuthor().getName();
if (!this.getRepliedTo()) {
return `${this.message} by ${authorName}`;
}
return this.getString(this);
}
I'm trying to meet the following conditions :
When converting to a string (toString), the following format is used:
No replied to:
message + " by " + author.name
With replied to:
message + " by " + author.name + " (replied to " + repliedTo.author.name + ")"
But i keep failing the test with this message:
The toString method should return the correct hierarchy (no reply)
const msg1 = {
fname:'Divyesh',
lname:'Patel',
}
const person = {
fullname : function(city,num)
{
return this.fname+ " " + this.lname+" "+ this.city +" "+ this.num ;
}
}
console.log(person.fullname.call(msg1,"Surat","972737"));
output :
Divyesh Patel undefined undefined
i write code in es6 and used call method but additional arguments are not passing it shows undefined
const msg1 = {
fname: 'Divyesh',
lname: 'Patel',
}
const person = {
fullname: function (city, num) {
return this.fname + " " + this.lname + " " + city + " " + num;
}
}
console.log(person.fullname.call(msg1, "Surat", "972737"));
I was wondering how can I make it posible to get rid of putting "new" before a function, for example:
new functionToDo("thingsToDo").iGotYouBruh("Halo Humans");
is there a posible way of doing this without the "new"?
here is the code I'm trying to use without the "new":
function local (title) {
var storeTitle = title;
this.addL = function(lString) {
var storeText = lString;
localStorage.setItem(storeTitle, storeText);
console.info("Locally stored " + storeTitle.toUpperCase() + " with " + storeText.substring(0, 10) + "... As text.");
};
this.removeL = function() {
localStorage.removeItem(storeTitle);
console.info("Locally removed " + storeTitle + ".");
};
this.getL = function () {
localStorage.getItem(storeTitle);
console.info("Locally got string of " + storeTitle + ": " + localStorage.getItem(storeTitle));
};
};
and here's what I would have to do to invoke the function:
new local("storedElement").getL();
This is possible by checking whether this is an instance of the function itself and returning a new instance otherwise:
function local (title) {
if (!(this instanceof local)) {
return new local(title);
}
var storeTitle = title;
this.addL = function(lString) {
var storeText = lString;
localStorage.setItem(storeTitle, storeText);
console.info("Locally stored " + storeTitle.toUpperCase() + " with " + storeText.substring(0, 10) + "... As text.");
};
this.removeL = function() {
localStorage.removeItem(storeTitle);
console.info("Locally removed " + storeTitle + ".");
};
this.getL = function () {
localStorage.getItem(storeTitle);
console.info("Locally got string of " + storeTitle + ": " + localStorage.getItem(storeTitle));
};
};
You could use JavaScript closures. In particular look at the "Using Closures for the Module Pattern" section of this webpage for a full description. The idea is to have the function return an literal with all the required methods. Any functions or variables that you want to be kept private are just local variables for the function.
I'm updating my knowledge about JavaScript and I stuck on one lesson task.
I have API that is returning string...
API.workerName = function (worker) {
return worker.firstName + ' ' + worker.lastName;
};
The task is to prefix returning string and not change API, but extend it. I also have to avoid copying & pasting code, because 3rd party code can change. I should re-use it instead.
What I did is change this function after loading API...
API.workerName = function (worker) {
return '(' + worker.position + ') ' + worker.firstName + ' ' + worker.lastName;
};
... but I think I did it wrong.
To extend the method, you should save the old definition and call it from your extension:
API.oldWorkerName = API.workerName;
API.workerName = function(worker) {
return '(' + worker.position + ')' + API.oldWorkerName(worker);
};
Or maybe this is what your lesson is looking for:
API.workerPositionAndName = function(worker) {
return '(' + worker.position + ')' + API.workerName(worker);
};
Another neat way to save the old definition and also make it unavailable to anybody else, would be to do something like this using IIFE to create a closure:
API.workerName = (function() {
var old = API.workerName; // this old version is only available inside your new function
return function(worker) {
return '(' + worker.position + ')' + old(worker);
}
})();
Here's an example:
API = {
workerName: function (worker) {
return worker.firstName + ' ' + worker.lastName;
}
};
API.workerName = (function () {
var old = API.workerName;
return function (worker) {
return '(' + worker.position + ')' + old(worker);
};
})();
alert(API.workerName({firstName: "Joe", lastName: "Blogs", position: "Lackey" }));
This question already has answers here:
How do you find out the caller function in JavaScript when use strict is enabled?
(5 answers)
Closed 2 years ago.
In framework, I'm developing, I've constructed mechanism, that allowed to define private and protected properties and methods.
The only ability, I found in ES5 specifications for doing that was using arguments.callee
like this:
descriptor.method = function () {
if (__callerIsProptected(arguments.callee.caller.caller, cls))
return value.apply(this, __defaults(_.values(arguments), defaults));
throw 'Attempt to call ' + access + ' method "' + cls._name + '::' + name + '"';
};
As far as in strict mode calls to arguments.callee and arguments.caller cause throwing of exceptions are there any convenient alternatives to do that?
Update - added whole called function code
function __descriptor(cls, type, name, descriptor, access) {
//protected private non-function descriptor.value is replaced by get/set pair
if (access !== 'public' && type == 'property') {
delete descriptor.value;
delete descriptor.writable;
_.isFunction(descriptor.get) || (descriptor.get = function () {
return this.__get(name);
});
_.isFunction(descriptor.set) || (descriptor.set = function (value) {
return this.__set(name, value);
});
}
//remove uselesses
if (_.isFunction(descriptor.get) || _.isFunction(descriptor.set)) {
delete descriptor.value;
delete descriptor.writable;
if (!_.isFunction(descriptor.get)) {
descriptor.get = function () {
return this.__get(name);
};
}
if (!_.isFunction(descriptor.set)) {
descriptor.set = function (value) {
return this.__set(name, value);
};
}
} else {
delete descriptor.get;
delete descriptor.set;
}
if (descriptor.get) {
var getter = descriptor.get;
//mutate getter and setter if given respectively to access level
if (access === 'public') {
descriptor.getter = function () {
return getter.apply(this, arguments);
};
} else if (access === 'protected') {
descriptor.getter = function () {
if (__callerIsProptected(arguments.callee.caller.caller, cls))
return getter.apply(this, arguments);
throw 'Attempt to get ' + access + ' property "' + cls._name + '::' + name + '"';
};
} else if (access === 'private') {
descriptor.getter = function () {
if (__callerIsPrivate(arguments.callee.caller.caller, cls))
return getter.apply(this, arguments);
throw 'Attempt to get ' + access + ' property "' + cls._name + '::' + name + '"';
};
}
descriptor.getter._class = cls;
}
if (descriptor.set) {
var setter = descriptor.set;
//mutate getter and setter if given respectively to access level
if (access === 'public') {
descriptor.setter = function () {
return setter.apply(this, arguments);
};
} else if (access === 'protected') {
descriptor.setter = function () {
if (__callerIsProptected(arguments.callee.caller.caller, cls))
return setter.apply(this, arguments);
throw 'Attempt to set ' + access + ' property "' + cls._name + '::' + name + '"';
};
} else if (access === 'private') {
descriptor.setter = function () {
if (__callerIsPrivate(arguments.callee.caller.caller, cls))
return setter.apply(this, arguments);
throw 'Attempt to set ' + access + ' property "' + cls._name + '::' + name + '"';
};
}
descriptor.setter._class = cls;
}
if (descriptor.value !== undefined) {
if (!_.isFunction(descriptor.value)) return descriptor;
var value = descriptor.value;
var defaults = descriptor.defaults || [];
if (access === 'public' && type == 'method') {
descriptor.method = function () {
return value.apply(this, __defaults(_.values(arguments), defaults));
};
} else if (access === 'protected') {
descriptor.method = function () {
if (__callerIsProptected(arguments.callee.caller.caller, cls))
return value.apply(this, __defaults(_.values(arguments), defaults));
throw 'Attempt to call ' + access + ' method "' + cls._name + '::' + name + '"';
};
} else if (access === 'private') {
descriptor.method = function () {
if (__callerIsPrivate(arguments.callee.caller.caller, cls))
return value.apply(this, __defaults(_.values(arguments), defaults));
throw 'Attempt to call ' + access + ' method "' + cls._name + '::' + name + '"';
};
}
descriptor.method._class = cls;
}
return descriptor;
}
Once I was developing same framework (abandoned) and the only way to figure out a caller in strict mode was to actually throw an exception and RegExp caller name from stack trace. As far as I remember it wasn't always precise. Look for example at the code of caller-id script