This question already has answers here:
Extend primitives without prototyping them
(2 answers)
Closed 7 years ago.
I've few variables that are of type number (or for that matter strings too).
I want add some metadata to these variables.
When I try to use Object.defineProperty(myvar, "prop", {/*getter & setter*/}) it gives an error that myvar is not an object.
How can I define a property on non-objects with getter and setter methods?
I don't want to add anything to Number.prototype because I want these properties only on few variables.
You can do this by prototypal inheritance.
Using Number.prototype like this:
var proto = Object.create(Number.prototype);
function MyNumber() {
}
MyNumber.prototype = proto;
MyNumber.prototype.constructor = MyNumber;
MyNumber.prototype.someOwnMethod = function () {
}
// and so on
var myVar = new MyNumber();
Object.defineProperty(myvar, "prop", {/*getter & setter*/});
You'll get an Object in prototypes along with methods of Number.
Or if you do not need any own methods, just use Number constructor instead of number literal.
var myVar = new Number();
Object.defineProperty(myvar, "prop", {/*getter & setter*/});
The same for strings:
var myVar = new String();
Related
This question already has answers here:
Assigning prototype methods *inside* the constructor function - why not?
(6 answers)
Closed 5 years ago.
I have writtern this two different ways and both giving me same result , so which I can use when ?
FIRST EXAMPLE
var BaseCls = function() {
BaseCls.prototype.name = "John";
};
var JustCls = new BaseCls();
console.log(JustCls.name); // This is giving result John
SECOND EXAMPLE
var BaseCls = function() {};
BaseCls.prototype.name = "John";
var JustCls = new BaseCls();
console.log(JustCls.name); // This is also giving result John
Both giving me same result so I just want to know is there any other criteria which lead to write this property / method with prototype inside / outside main function ?
Thanks for consideration
You should change prototype only outside the constructor.
Otherwise you change it every time you create an instance.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
creating objects - new object or object literal notation?
Literal notation VS. constructor to create objects in JavaScript
I'm going through my very first Javascript tutorial.
I just found two ways to create a JS object.
var person = new Object();
person.name = "Tom";
person.age = "17";
and
var person = {};
person.name = "Tom";
person.name = "17"
Any difference between these two ways of object creation? Since the second looks simpler, can we always use it under any condition?
Not only is the second syntax easier to read and not only will it work under any condition, but the first syntax might not work under all conditions:
function Object() {
// Oh crap, we have redefined Object!
return []; // return an array because we are EVIL
}
var person = new Object(); // not what we think it is
But {}, being a syntactic construct, is immune to such evil trickery.
In addition, the object literal notation can be partially optimized at parse time, since after all there's only one object type that could be created. That may result in a minuscule performance increase.
This question already has answers here:
Javascript when to use prototypes
(6 answers)
Closed 7 years ago.
What is the difference between making a method using prototype and just creating it inside a function? E.g., what's the difference between methodOne and methodTwo below?
function myFunc() {
this.methodOne = function () { console.log("First method.")};
}
myFunc.prototype.methodTwo = function () { console.log("Second method.")};
They seem to behave the same way:
var myFuncInstance = new myFunc();
myFuncInstance.methodOne();
myFuncInstance.methodTwo();
But my sense is that methodTwo, by accessing the prototype directly, is doing something a little different.
The difference is that every instance of myFunc shares the same single instance of methodTwo, but has its own instance of methodOne.
I.e.
var foo = new myFunc();
var bar = new myFunc();
foo.methodOne === bar.methodOne; // false
foo.methodTwo === bar.methodTwo; // true
Taking this a bit further, if you are creating 1000 myFunc instances, you are creating 1000 methodOne functions that all do the same thing. There still would be only one methodTwo function.
With using the .prototype syntax you can add methods to existing objects. For example you can add a sum method to the array type using
Array.prototype.sum = function() {...}
Then use it as
var a = [1,2,3] and then doing a.sum()
With .prototype you can just keep on extending based on your needs just as you would in a class based language.
This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 8 years ago.
Having this code:
var Person = function(_name){
this.name = _name;
}
Person.prototype.surname = 'Jiménez';
What's the difference between this.name and Person.prototype.surname, what changes when inherited? what's the difference then when:
var newPerson = new Person('Carlos');
The difference is where the property is defined,
.name is defined on the actual newPerson object while .surname is defined on newPerson's prototype object.
if you were to create 2 Person objects the two will share the same .surname property but each will have its own .name property.
notice that once you assign a different value to .surname on an object like this
newPerson.surname = 'some name';
you actually defines a completely new property on the object. the only way to go back to view the prototype value is to do
delete newPerson.surname;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
creating objects - new object or object literal notation?
Literal notation VS. constructor to create objects in JavaScript
I'm going through my very first Javascript tutorial.
I just found two ways to create a JS object.
var person = new Object();
person.name = "Tom";
person.age = "17";
and
var person = {};
person.name = "Tom";
person.name = "17"
Any difference between these two ways of object creation? Since the second looks simpler, can we always use it under any condition?
Not only is the second syntax easier to read and not only will it work under any condition, but the first syntax might not work under all conditions:
function Object() {
// Oh crap, we have redefined Object!
return []; // return an array because we are EVIL
}
var person = new Object(); // not what we think it is
But {}, being a syntactic construct, is immune to such evil trickery.
In addition, the object literal notation can be partially optimized at parse time, since after all there's only one object type that could be created. That may result in a minuscule performance increase.