what is the difference between these 2 javascript functions? [duplicate] - javascript

This question already has answers here:
What's the difference between this.bla to Object.prototype.bla
(4 answers)
Closed 9 years ago.
I want to know what is the difference between insideFn and outsideFn in the follwoing case :
function Construct()
{
this.insideFn = function(obj){
....
}
}
Construct.prototype.outsideFn = function(obj){
...
}
and which one is efficient to use ?

In the first case every instance created with Construct will have its own insideFn, which will waste memory in case you will use more than one instance. In the second case each instance of Construct will have just a reference to outsideFn. So the latter is better.

In the first construct the method is an attribute of the instance of Construct.
In the second construct the method is an attribute of the prototype object from Construct
If you define functions inside the prototype object, they will be the same when calling.
The prototypevariant often saves memory and speeds up the code.
You can also overload a prototype function within the instance of an object, to change the default behaviour of your object.
For better english and much more details see http://www.javascriptenlightenment.com/

The first one function Construct() {} is a class(in javascript it is an object) whereas later on you are inheriting(extending) this class using the keyword prototype and adding one more function outsideFn. There are no specific classes in JavaScript so you have to use objects as classes.

Related

Can new be applied to any function in JavaScript? [duplicate]

This question already has answers here:
How to check if a Javascript function is a constructor
(9 answers)
Closed 1 year ago.
Many sources like MDN and this tutorial define a concept called "constructor" that should be used with the new keyword to create an instance in traditional OOP's sense. But what "constructor" means is not formally stated.
It seems to me that literally any function can be used with new. (Though function without any manipulation to this in its definition is not particularly useful when newed, because it merely returns an empty object). Is this correct?
When you use the new keyword, it creates a new object. When you create an instance of a class with new, it creates an object, and any properties in the constructor method will be initialized in the object as well.

What is constructor? What kind of function object can be called constructor? [duplicate]

This question already has answers here:
Constructor function vs Factory functions
(8 answers)
Closed 5 years ago.
I'm learning javascript and I'm confused with this definition.
I look up in ECMA and it defines constructor as
function object that creates and initializes obejects.
So, can any function object be called constructor?
In js its common to call a function constructor if its aim is to be called with the new operator:
var me = new Human;//Human is a constructor
However, human languages are not that strictly defined, so you can probably use it always, you just need to have good arguments for your usage. A good arguable case:
function Animal(name){//actually rather a factory function
return {name};
}
var cat = new Animal("bob");//and now ? :/
So, can any function object be called constructor?
But not every function creates or initializes objects. Consider this example:
function add(a, b) {
return a + b;
}
This function only adds two values.
What is constructor? What kind of function object can be called constructor?
I'd argue that only functions that are intended to be called with new are supposed to be considered "constructors" (that includes functions created via class).
However, you can also create objects without calling a function with new:
function getPerson(name) {
return {name: name};
}
Whether or not you consider such functions constructors is probably subjective.

Clever JavaScript to bypass eval method [duplicate]

This question already has an answer here:
What's going on in this piece of Javascript?
(1 answer)
Closed 6 years ago.
[]["constructor"]["constructor"](<string representing JavaScript code>)()
In JavaScript the "constructor" property returns the prototype of an object. In this case the prototype of [] is the Array class. Accessing the "constructor" property of the Array class returns the Function object. The constructor of Function object then returns a function and the body of that function is the last parameter, which is passed to the constructor. This results in the creation of a function that uses the provided string as the function's body (i.e. code), which is then instantly executed.
As stated in the paragraph above from: https://www.trustwave.com/Resources/SpiderLabs-Blog/Angler-Exploit-Kit-%E2%80%93-Gunning-For-the-Top-Spot/?page=1&year=0&month=0
The above line of code was used to execute obfuscated JavaScript code without using the 'eval' method. After reading this paragraph, I can't quite grasp this clever line of code. Can anyone explain what is actually happening?
Note the constructor of an Array instance is obviously Array:
[].constructor === Array
and further, the constructor of Array is Function:
[].constructor.constructor === Array.constructor === Function
Now in JavaScript, Function(source) returns a function instance whose source is given by the parameter. For example:
Function("alert(1337)");
will create (and is analogous to):
function() {
alert(1337);
}
Your code will instantiate such a function and immediately call it with (). And that's exactly how eval behaves.
So, if it helps, you could reduce your code example to:
Function(source)();

the difference of javascript class inheritance [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
this Vs. prototype [duplicate]
(1 answer)
Closed 9 years ago.
What is the difference of inheritance definition of the two ways below
function Rectangle(w,h){
this.width=w;
this.height.h;
this.area=function(){return this.width*this.height;}
}
and
function Rectangle(w,h){
this.width=w;
this.height.h;
}
Rectangle.prototype.area=function(){return this.width*this.height;}
I saw somebody said the first way is inefficient of use regular properties for methods that are intended to be shared by all objects of the same class.
Welcome any comment
The first way, you could use w and h directly inside the area function, equals to use them as private variable.
function Rec(w,h) {
this.setW=function(newW){
w=newW;
}
this.area=function(){
return w*h;
}
}
var rec=new Rec(5,6);
you cannot do alert(rec.w), or rec.w=5. since there is no this.w inside the class.
but you can do
rec.setW(2);
alert(rec.area());
this will alert 12.
http://jsfiddle.net/vanessachem/Hmyyc/ like this. w and h could be treat private variable. they could only be reset via a setter function inside the class.
It is inefficient when you need to create multiple instance. If you just want to create singleton, the first one is easy to manage.
The advantage of the second one is that you could put prototype function inside different file. It is good for multiple instances. However, you cannot treat w and h as private variable. You can't use w or h directly in the area function.
The first way, every time you construct a new Rectangle, you also create a new anonymous function and assign it to this.area. The second way is more efficient if you are going to be constructing more than one Rectangle, because the anonymous function is still only created once, and all Rectangles have access to it, via their prototype.

Javascript Call And Apply used together [duplicate]

This question already has answers here:
call and apply in javascript [duplicate]
(2 answers)
Closed 8 years ago.
I know the call and apply in javascript but how does exactly the difference between javascript Call and Apply..?? and another thing i found some code use this together like this:
function doSomething() {
return Function.prototype.call.apply(Array.prototype.slice, arguments);
}
are is the same as..
Array.prototype.slice.apply(arguments)
Why we want to use call and apply together?
No, it is not the same. Array.prototype.slice.apply(arguments) applies the slice function on the current argument object, while Function.prototype.call.apply(Array.prototype.slice, arguments); calls the slice function on the array which is provided as the first argument.
Maybe things like this will become easier with new EcmaScript syntax. Your doSomething is equivalent to
function doSomething(array, ...)
array.slice(...); // assuming array is really an array
}
whereas the second is equivalent to
function (...) {
arguments.slice(); // assuming argument objects are actual arrays
}
ase the same
Array.prototype.slice.apply(arguments)
yes, i think its the same too..!!

Categories