I want to namespace my code, so I did this:
let Namespace = {};
Namespace.Func = function (a, b) {
this.a = a;
this.b = b;
};
Namespace.Func.prototype.getSum = function () {
return this.a + this.b;
};
Then, I created an instance of Namespace.Func:
let f = new Namespace.Func(1, 2);
Now , I would expect all these lines to be true:
console.log(f.getSum() === 3);
console.log(typeof f === 'object');
console.log(f instanceof Object);
console.log(f instanceof Namespace.Func);
console.log(f.constructor === Namespace.Func);
console.log(f.constructor.name === "Namespace.Func");
But the last one is false, because f.constructor.name is "".
Why is that? Can it be fixed?
Here you have the code snippet:
let Namespace = {};
Namespace.Func = function (a, b) {
this.a = a;
this.b = b;
};
Namespace.Func.prototype.getSum = function () {
return this.a + this.b;
};
let f = new Namespace.Func(1, 2);
console.log("f.getSum() === 3", f.getSum() === 3);
console.log("typeof f === 'object'", typeof f === 'object');
console.log("f instanceof Object", f instanceof Object);
console.log("f instanceof Namespace.Func", f instanceof Namespace.Func);
console.log("f.constructor === Namespace.Func", f.constructor === Namespace.Func);
console.log("f.constructor.name === 'Namespace.Func'", f.constructor.name === 'Namespace.Func');
console.log('---');
console.log("f.constructor.name", f.constructor.name);
console.log("f.constructor.name === ''", f.constructor.name === '');
Specify function name for your constructor like below:
Namespace.Func = function TheNameOfConstructor (a, b) {
this.a = a;
this.b = b;
};
The assert will pass after that like this:
console.log(f.constructor.name === "TheNameOfConstructor");
Related
So the task is :
1. If the argument c is passed and it is a function, it is executed after calling the sum function.
2. The function f must return the result of the function of argument c, if any, or the result of the function sum.
Here is my code.
function c(){
return console.log('c is a function');
}
function f (a = 2, b = 3, c){
if(typeof c === 'function'){
c();
} else if (typeof c === 'undefined'){
return;
} else {
function sum (a, b){
return a + b;
}
}
}
f(5, 10);
It stopped working once I added this line:
else if (typeof c === 'undefined'){
return;
}
I would appreciate if you provide your pieces of advice as I am a newbie :)
Well, it should be as expected?
When using that function without the 3 parameter, the c param is undefined.
You can verify that by logging it
...
else if (typeof c === 'undefined'){
console.log(typeof c)
return;
}
...
But there doesn't seem to be a need for that check.
F.e.
function c(){
return 'c is a function';
}
function f (a = 2, b = 3, f = null){
if(typeof f === 'function'){
return f(a, b);
}
else {
return a + b;
}
}
console.log(f(5, 10))
console.log(f(5, 10, 999))
console.log(f(6, 10, c))
Are you looking for something like this?
function sum(a, b){
return a + b;
}
function f (a = 2, b = 3, c){
if(typeof c === 'function'){
return c( sum(a,b) );
} else {
return sum(a,b);
}
}
var num1 = f(5, 10 );
var num2 = f(5, 10, ( num ) => num * 5 );
Here num1 gives you 15, and num2 gives you 75
I have below code in node -
function ABC(a,b,c) {
this.a = a;
this.b = b;
this.c = c;
this.equals = function(other) {
return other.a == this.a &&
other.b == this.b &&
other.c === this.c;
};
}
var a1 = new ABC("1", "1", 0.94924088690462316);
var a2 = new ABC("1", "1", 0.94924088690462316);
console.log(a1 === a2);
var arr = [a1];
console.log(arr.includes(a2));
This code outputs is -
false
false
how can I check whether the array includes the specific object is true?
Since it look like you're trying to check whether the objects contain the same values, see that you've already defined an equals method - just use it:
function ABC(a,b,c) {
this.a = a;
this.b = b;
this.c = c;
this.equals = function(other) {
return other.a == this.a &&
other.b == this.b &&
other.c === this.c;
};
}
var a1 = new ABC("1", "1", 0.94924088690462316);
var a2 = new ABC("1", "1", 0.94924088690462316);
console.log(a1.equals(a2));
You can make the code more efficient by defining the method on the prototype:
function ABC(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
ABC.prototype.equals = function(other) {
return other.a == this.a &&
other.b == this.b &&
other.c === this.c;
};
var a1 = new ABC("1", "1", 0.94924088690462316);
var a2 = new ABC("1", "1", 0.94924088690462316);
console.log(a1.equals(a2));
Also, keep in mind that 0.94924088690462316 holds too many significant figures for Javascript to handle - that number will be stored as 0.9492408869046232:
console.log(0.94924088690462316);
Only slightly different from Snow's answer:
function ABC(a,b,c) {
this.a = a;
this.b = b;
this.c = c;
this.equals = function(other) {
return other.a == this.a &&
other.b == this.b &&
other.c === this.c;
};
}
var a1 = new ABC("1", "1", 0.94924088690462316);
var a2 = new ABC("1", "1", 0.94924088690462316);
console.log(a1.equals(a2));
var arr = [a1];
console.log(arr.some(abc => (a1.equals(abc))));
So I have this IIFE, and for the sake of consistence with other ES6 classes I rewrote for a plugin, I also want to rewrite this into using ES6 Class syntax. Can anyone show me how to do it?
Foo = (function(){
Foo.bar = function(a, b){
baz = new this(a, b);
return baz;
}
function Foo(a, b){
this.a = a;
this.b = b;
}
return Foo;
})();
It would be a simple
class Foo {
constructor(a, b) {
this.a = a;
this.b = b;
}
static bar(a, b) {
return new this(a, b);
}
}
The function created by the IIFE can be just as well created using a plain function declaration and property assignment:
function Foo(a, b) {
this.a = a;
this.b = b;
}
Foo.bar = function(a, b) {
baz = new this(a, b);
return baz;
}
var foo = new Foo('FooA', 'FooB');
console.log(foo.a + ':' + foo.b);
var baz = Foo.bar('BazA', 'BazB');
console.log(baz.a + ':' + baz.b);
Using the class syntax:
class Foo {
constructor(a, b) {
this.a = a;
this.b = b;
}
static bar(a, b){
baz = new this(a, b);
return baz;
}
}
var foo = new Foo('FooA','FooB');
console.log(foo.a + ':' + foo.b);
var baz = Foo.bar('BazA', 'BazB');
console.log(baz.a + ':' + baz.b);
Unfortunately I can't actually run this code as the site I'm currently on has SOE versions of Firefox and IE that don't recognise the class syntax. So please update if it's faulty.
Sometimes I stared at js provided with google.com main page and found that they tended to use (0, obj.func)(args) syntax. Here are excerpts from the script:
var _ = _ || {};
(function (_) {
var window = this;
try {
_.mb = function (a) {
return (0, window.decodeURIComponent)(a.replace(/\+/g, " "))
};
_.zg = function (a, b) {
for (var c = a.length ? a.split("&") : [], d = 0; d < c.length; d++) {
var e = c[d];
if ((0, _.Ag)(e) == b) return (c = /=(.*)$/.exec(e)) ? (0, _.mb)(c[1]) : null
}
return null
};
_.Ag = function (a) {
return (a = /^(.+?)(?:=|$)/.exec(a)) ? (0, _.mb)(a[1]) : null
};
var Cg = function (a, b) {
var c = a.indexOf("?");
return 0 > c ? null : (0, _.zg)(a.substring(c + 1), b)
};
// Note var Cg called with no 0
var oca = function (a) {
this.A = Cg(a, "mods");
this.B = Cg(a, "ver")
};
} catch (e) {}
})(_);
Why prepending 0?
This makes an indirect call.
This ensures the context, in the called function, is the global one. This might be useful in an internal scope.
Example :
var a = {
b: function(){
console.log(this);
},
c1: function(){
this.b();
},
c2: function(){
(0, this.b)();
},
c3: function(){
(this.b)();
}
}
a.c1(); // logs a
a.c2(); // logs window
a.c3(); // logs a
I have an nested object that I want to update it with values provided by object that contains similar structure but only the properties that I want updated. Creating a new result instead of modifying the initial objects is great too.
var initial =
{
a: 1,
b : {
c : 2,
d : 3
},
f: 5
};
var update = {
a: 2,
b: {
d: 2
}
};
function updateFunction (a,b) { return a+b;};
var result=
{
a: 3, // updateFunction (1,2)=> 3
b : {
c : 2,
d :5 // updateFunction (3,2) => 5
},
f: 5
};
Have not tested fully, but maybe,
assuming objects are simple as stated,
function updateFunction (a,b) { return a + b;};
function recurse(initial, update){
for(prop in initial){
if({}.hasOwnProperty.call(initial, prop) && {}.hasOwnProperty.call(update, prop)){
if(typeof initial[prop] === 'object' && typeof update[prop] === 'object'){
recurse(initial[prop], update[prop]);
}
else{
initial[prop] = updateFunction(initial[prop], update[prop]);
}
}
}
}
recurse(initial, update);
EDIT
If result is expected without changing initial
function updateFunction (a,b) { return a + b;};
function recurse(initial, update){
var result = {};
for(prop in initial){
if({}.hasOwnProperty.call(initial, prop)){
result[prop] = initial[prop];
if({}.hasOwnProperty.call(update, prop)){
if(typeof initial[prop] === 'object' && typeof update[prop] === 'object'){
result[prop] = recurse(initial[prop], update[prop]);
}
else{
result[prop] = updateFunction(initial[prop], update[prop]);
}
}
}
}
return result;
}
var result = recurse(initial, update);
hope this helps.
Here's how I'd do it:
// The parallel to Array.map
Object.map = function(obj, f) {
var result = {};
for(k in obj)
if({}.hasOwnProperty.call(obj, k))
result[k] = f(k, obj[k]);
return result;
}
// Takes two objects and uses `resolve` to merge them
function merge(a, b, resolve) {
return Object.map(a, function(k, a_value) {
if(k in b)
return resolve(a_value, b[k]);
else
return a_value;
});
}
// same as above, but recursing when an object is found
function recursive_merge(a, b, resolve) {
return merge(a, b, function(a_value, b_value) {
if(typeof a_value == 'object' && typeof b_value == 'object')
return recursive_merge(a_value, b_value, resolve);
else
return resolve(a_value, b_value);
});
}
result = recursive_merge(initial, update, function(a, b) { return a + b; })