const person = Object.create(null); person.name = 'Philip'; person.age = 18;
console.log(person.toString()); // TypeError: person.toString is not a function console.log(person.hasOwnProperty('name')); // TypeError: person.hasOwnProperty is not a function console.log('name'in person); // true
Posted onEdited onInjavascript Symbols count in article: 338Reading time ≈1 mins.
TypeError: Cannot read properties of undefined (reading ‘xxx’)
This error occurs when you try to access a property of an object that is undefined.
1 2
let person = undefined; console.log(person.age); // Uncaught TypeError: Cannot read properties of undefined (reading 'age')
ReferenceError: a is not defined
This is the most common error in JavaScript. It means that you are trying to use a variable that has not been declared.
1
console.log(a); // Uncaught ReferenceError: a is not defined
ReferenceError: Cannot access ‘a’ before initialization
This error occurs when you try to access a variable before it is declared. After ES6, this means you touch the (TDZ)temporal dead zone. temporal dead zone is the zone between the start of the block scope and the actual declaration of the variable.
1 2
console.log(a); // Uncaught ReferenceError: Cannot access 'a' before initialization const a = 1;
TypeError: Assignment to constant variable.
This error occurs when you try to reassign a value to a constant variable.
1 2
const a = 1; a = 2; // Uncaught TypeError: Assignment to constant variable.
RangeError: Maximum call stack size exceeded
See the following code, why it cause the RangeError: Maximum call stack size exceeded error?
Posted onEdited onInjavascript Symbols count in article: 2kReading time ≈7 mins.
Introduction
What is a closure in JavaScript? This is really a hard question, and I see lots of different answers on the internet. I list some of them below:
A closure is a function that has access to the outer function’s variables.
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives a function access to its outer scope. In JavaScript, closures are created every time a function is created, at function creation time. - from MDN
In the follow example, the inner function increase has access to the outer function’s variable count. The variable count is private and cannot be accessed from outside the function createCounter. But you can still change count by invoking the increase function.
We can also return the function increase directly.(This is not work when you have multiple functions to return, you must use the object literal in that case.)
1 2 3 4 5 6 7 8
functioncreateCounter() { let count = 0;
returnfunction () { count++; return count; } }
And you should also change the way to invoke the function since you return an anonymous function. so there is no increase property in the counter object.
In the following example, we can simulate a module by using closure. The private variables and methods are defined inside the outer function MyModule. The public variables and methods are returned as an object literal. So the private variables and methods are encapsulated and cannot be accessed from outside the function. The public variables and methods can be accessed by invoking the returned object.
// If you want to expose the variables/methods, return them. return { publicVariable, publicMethod, }; }
const myModule = MyModule(); console.log(myModule.publicVariable); // public variable myModule.publicMethod(); // public method
Event handler(or callback)
Some event handler or callback functions use closure to access the outer function’s variables. In the following example, the event handler for click event is an anymous function. The function has access to the outer function’s variable count.
In the following code, we use a local variable instance to check whether the instance has been created or not. The instance variable must memorize its state during different calls to the getInstance function. So we use closure to achieve this.
document.querySelector("#input").addEventListener( "input", debounce(function (e) { document.querySelector("#output").innerHTML = "You have input: " + e.target.value; // You can call and server side api here, and debounce make sure the api is not called very often. }, 500) );
Posted onEdited onInbrowser Symbols count in article: 386Reading time ≈1 mins.
Introduction
Reflow and repaint are two important concepts in the browser rendering process. Understanding them can help us optimize the performance of our web pages.
Reflow
Reflow is the process of recalculating the position and size of all elements in the DOM tree. When the layout of the page changes, the browser needs to reflow the page to recalculate the position and size of all elements. The following changes can trigger a reflow:
Resizing the window
Changing the width or height of an element
Changing the padding, margin, or border of an element
Changing the font size of an element
Changing the position of an element
Repaint
Repaint is the process of updating the pixels on the screen. When the layout of the page changes, the browser needs to repaint the affected elements to update the pixels on the screen. The following changes can trigger a repaint:
Changing the background color of an element
Changing the text color of an element
Changing the visibility of an element
Changing the opacity of an element
Changing the z-index of an element
How Browser render pages
The browser rendering process consists of several steps:
Parse HTML: The browser parses the HTML code and creates a DOM tree.
Parse CSS: The browser parses the CSS code and creates a CSSOM tree.
Combine DOM and CSSOM: The browser combines the DOM tree and CSSOM tree to create a render tree.
Layout: The browser calculates the position and size of all elements in the render tree.
Paint: The browser paints the pixels on the screen based on the render tree.
Composite: The browser combines the painted pixels to create the final image on the screen.
In the process of rendering a web page, the browser may need to trigger reflows and repaints to update the layout and appearance of the page.
Reflow: Recalculating the position and size of all elements in the DOM tree.
Repaint: Updating the pixels on the screen.
Reflow will trigger repaint, but repaint does not necessarily trigger reflow.
Posted onEdited onInangular Symbols count in article: 389Reading time ≈1 mins.
What is Angular Router Guard?
Router Guard is a feature in Angular Router that allows you to run some code before the route is activated. It can be used to protect routes from unauthorized access, redirect users to a login page, or perform any other action before the route is activated.
canActivate
Use case: Only allow authenticated users to access the /product page, otherwise redirect them to the login page.
isAuthenticated() { // Put authentication logic here returnfalse; } }
canActivateChild
Similar to canActivate, but it is used to protect child routes. In the following example, only authenticated users can access the child routes of the /product page.
Posted onEdited onInangular Symbols count in article: 361Reading time ≈1 mins.
This post will show you how to style the active link in Angular Router with routerLinkActive and routerLinkActiveOptions.
routerLinkActive
routerLinkActive is a directive that adds a CSS class to the element when the link’s route becomes active.
We set a class name active to the active link, and apply a red background color to the active link. When user click the home or about link, the background color of the active link will change to red.
routerLinkActiveOptions is an input property of routerLinkActive directive that allows you to set the options for the active link. It provides a finer-grained control over the behavior of the active link.
Let’s take a look at the definition of routerLinkActiveOptions:
With this settings, when user input /home#section1 in the browser, the home link will not be active, since the fragment is not exactly the same as the link’s fragment.
You can also config the matrixParams, queryParams, and paths in the IsActiveMatchOptions object to control the active link behavior.
Downgrade the JDK version to previous version, for example jbr-17.
The reason that old version still not work is because after upgrade Android Studio to LadyBug, it automatically set Java 21.0.3 and Gradle 8.0. Even if you downgrade the Android Studio, the settings still remain the same. So you need to manually change the settings to make it work.
Posted onEdited onInandroid Symbols count in article: 110Reading time ≈1 mins.
Introduction
There are two types of SDK location in Android, global SDK location which is used by all projects and project SDK location which is used by a specific project.
Global SDK Location
In Android Studio, open File | Settings | Languages & Frameworks | Android SDK. Here you can see the global SDK location.
Project SDK Location
Open Android Studio, go to File -> Project Structure -> SDK Location. Here you can see the project SDK location.
If your project has file local.properties, you can specify the project SDK location by adding sdk.dir=/path/to/android/sdk in the file. For example: