Saturday, December 2, 2017
Sunday, November 19, 2017
Apex: Monitor / manage heap during execution
Use the code below in your Apex code to monitor the heap during execution.
System.debug(LoggingLevel.Debug, 'Heap Size: ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
Reference: https://help.salesforce.com/articleView?id=000170956&type=1
Saturday, November 18, 2017
Javascript: Pattern to create instance-specific private data
To make a property private, just make it local to the constructor. Any outside code trying to access
age
variable will result in an undefined
. But the outside code can still call the property that accesses this private variable using the public function getAge
.
function Person(name) {
// Define a variable only accessible inside of the Person constructor
var age = 25;
this.name = name;
this.getAge = function() {
return age;
};
this.growOlder = function() {
age++;
};
}
var person = new Person("Nicholas");
console.log(person.name); // "Nicholas"
console.log(person.getAge()); // 25
person.age = 100;
console.log(person.getAge()); // 25
person.growOlder();
console.log(person.getAge()); // 26
Javascript: Predict the value of this argument
this is determined by the calling context, the way in which function is called.
Calling standalone functions directly
When a function is executed as a simple object function directly, this refers to the global object.function foo() {
console.log('Hello World!');
console.log(this);
}
foo();
Calling functions as property of an object reference
Here we have created an object obj, and there is a property on it which is a function. The function is being called as a property on the object. Here this refers to the object obj itself.var obj = {};
obj.foo = function() {
console.log('Hello World!');
console.log(this);
}
obj.foo();
Calling standalone functions using 'new' keyword
When a function is called using the new keyword, this always refer to the newly created object, an empty object.function foo() {
console.log('Hello World!');
console.log(this);
}
new foo();
Friday, November 17, 2017
Javascript: Identifying Primitive and Reference Types
Identifying Primitive Types
There are five primitive types in JavaScript:- boolean
- number
- string
- null
- undefined
The best way to identify primitive types is with the typeof operator, which works on any variable and returns a string indicating the type of data.
console.log(typeof "John Doe"); // "string"
console.log(typeof 10); // "number"
console.log(typeof 5.1); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
When you run typeof null, the result is "object". The best way to determine if a value is null is to compare it against null directly, like below. Make sure that when you’re trying to identify null, use triple equals so that you can correctly identify the type.
console.log(value === null); // true or false
Identifying Reference Types
The built-in reference types are- Array
- Date
- Error
- Function
- Object
- RegExp
A function is the easiest reference type to identify because when you use the typeof operator on a function, the operator should return "function":
function reflect(value) {
return value;
}
console.log(typeof reflect); // "function"
Other reference types are trickier to identify because, for all reference types other than functions, typeof returns "object". To identify reference types more easily, you can use JavaScript’s instanceof operator. The instanceof operator takes an object and a constructor as parameters. When the value is an instance of the type that the constructor specifies, instanceof returns true; otherwise, it returns false.
var items = [];
var object = {};
function reflect(value) {
return value;
}
console.log(items instanceof Array); // true
console.log(object instanceof Object); // true
console.log(reflect instanceof Function); // true
Array.isArray() is the best way to identify arrays.
var items = [];
console.log(Array.isArray(items)); // true
Subscribe to:
Posts (Atom)