This question already has answers here:
Add a return type of string when a function is accessed like a variable
(2 answers)
Harmony proxy, detect whether property was accessed or called
(2 answers)
Closed 3 years ago.
I want to change styles and get them, using this method:
style(element).color("red") // Change color to red
style(element).color // get current color
This is what I tried:
let styleProxy = {
get: (object, property) => {
return (value) => {
object[property] = value;
return new Proxy(object, styleProxy);
}
return object[property];
}
}
let style = (element) => {
return new Proxy(element.style, styleProxy);
}
let element = document.getElementsByClassName("test")[0];
let test = style(element)
.color;
I know, that these functions are created dynamically but how do I check if I'm calling this proxy function-based or property-based.
Related
This question already has answers here:
Why can I access TypeScript private members when I shouldn't be able to?
(10 answers)
Closed 2 years ago.
class Animal {
privateAttribute
setPrivateAttribute(value) {
this.privateAttribute = value
}
}
new Animal().setPrivateAttribute('good way') //ok
new Animal().privateAttribute = 'not allowed' // no
I want to prevent update privateAttribute directly, the only way to set privateAttribute is call setPrivateAttribute function. What shold I do?
Please put all the private variable inside your constructor.
class Animal {
constructor() {
let privateAttribute = 'default';
this.setPrivateAttribute = newValue => {
privateAttribute = newValue
}
this.getPrivateAttribute = () => privateAttribute;
}
}
let newAnimal = new Animal()
// get variable value
newAnimal.getPrivateAttribute()
// Set new Value
newAnimal.setPrivateAttribute('New Value')
This question already has answers here:
Finding Variable Type in JavaScript
(12 answers)
Closed 2 years ago.
If I've a variable and I want to know what exactly is the type of object, how would I do that?
// this prints "object"
// any way to know what kind of object
// like whether a GlideRecord or Reference Field or array or json?
gs.log(typeof myVariable);
If GlideRecord and so on are classes or constructor functions, you can use the constructor property.
class GlideRecord {}
// or legacy constructor function -
// function GlideRecord() {}
const instance = new GlideRecord()
const { constructor } = instance
console.log(constructor) // GlideRecord
console.log(constructor.name) // "GlideRecord"
console.log(constructor === GlideRecord) // true
Note that this will not work if they're just plain old JS objects created with factory functions - in this case, constructor will simply be Object:
const createGlideRecord = () => {
return { /* ... */ }
}
const instance = createGlideRecord()
const { constructor } = instance
console.log(constructor) // Object
console.log(constructor.name) // "Object"
console.log(constructor === createGlideRecord) // false
This question already has answers here:
ES6 arrow functions not working on the prototype?
(4 answers)
Closed 3 years ago.
I am trying to validate if the file uploaded using input type = "file" is an image file or not in the following example:
function UploadImageProcess(ImageFile) {
this.file = ImageFile;
//... some other things
}
UploadImageProcess.prototype.fileTypeValidation = () => this.file.type.startsWith('image/') ? true : false;
//Other prototype methods
let target = document.getElementById('test');
target.addEventListener('change', (e) => {
let file = target.files[0];
let newProcess = new UploadImageProcess(file);
console.log(newProcess);
console.log(newProcess.fileTypeValidation());
});
<input id = "test" type = "file" accept = "image/*">
As you can see from my console.log(newProcess), my newProcess already contains the property file and yet when my prototype method tries to access said property, it says that it's undefined. What's the problem here?
becuse you use arrow function and bind to the wrong this, change it to normal function.
UploadImageProcess.prototype.fileTypeValidation = function(){
return this.file.type.startsWith('image/')
}
reply to comment, you can also bind this in constructor (not really the same thing, though)
function UploadImageProcess(ImageFile) {
this.file = ImageFile;
this.fileTypeValidation = () => this.file.type.startsWith('image/');
}
This question already has answers here:
Private properties in JavaScript ES6 classes
(41 answers)
Closed 5 years ago.
I want to build a class using javascript like in c, the main problem is private attribute.
var tree = {
private_var: 5,
getPrivate:function(){
return this.private_var;
}
};
console.log(tree.private_var);//5 this line want to return unaccessible
console.log(tree.getPrivate());//5
so I want to detect the access from tree.private_var and return unaccessible, and this.private_var return 5.
My question is: Is there any way to set private attribute in javascript?
EDIT: I saw this way
class Countdown {
constructor(counter, action) {
this._counter = counter;
this._action = action;
}
dec() {
if (this._counter < 1) return;
this._counter--;
if (this._counter === 0) {
this._action();
}
}
}
CountDown a;
a._counter is not accessible?
but
Define tree as a function instead of JavaScript object, define private variable in the function by var keyword, define public getting function by this. keyword and create a new instance by using the function
var Tree = function(){
var private_var = 5;
this.getPrivate = function(){
return private_var;
}
}
var test = new Tree();
test.private_var; //return undefined
test.getPrivate(); //return 5
In ES6, you can do this but it is not supported by IE so I wouldn't recommend
class Tree{
constructor(){
var private_var =5;
this.getPrivate = function(){ return private_var }
}
}
var test = new Tree();
test.private_var; //return undefined
test.getPrivate(); //return 5
This question already has answers here:
What techniques can be used to define a class in JavaScript, and what are their trade-offs?
(19 answers)
Closed 8 years ago.
I come from C, C++, Java background. I want to create a struct/ class with some attributes and methods like:
MyClass{
string text;
array characters;
printText(){
print text;
}
printLen(){
print characters.length();
}
}
So next time I can create an object like above by just calling its constructor. How do I implement this in JavaScript? I am new to the programming principles followed in javascript, I want to create a custom data type here for reusability.
function MyClass () {
this.text = "";
this.characters = [];
}
MyClass.prototype.printText = function () {
console.log(this.text);
};
MyClass.prototype.printLen = function () {
console.log(this.characters.length);
};
var instance = new MyClass();
instance.text = "test";
instance.characters.push('a');
instance.characters.push('b');
instance.printText();
instance.printLen();