Define a Method in JavaScript

seetha
Updated on 07-Jan-2020 08:07:26

485 Views

A method in JavaScript is the action performed on objects. A JavaScript method has a function definition, which is stored as a property value. ExampleLet’s see an example to define a method in JavaScriptLive Demo                                 var department = {                deptName: "Marketing",                deptID : 101,                deptZone : "North",                details : function() {                   return "Department Details" + "Name: " + this.deptName + " Zone: " + this.deptZone + "ID: " + this.deptID;                }             };          document.getElementById("myDept").innerHTML = department.details();          

Define Functions Inside a Function Body in JavaScript

mkotla
Updated on 07-Jan-2020 08:06:24

215 Views

To achieve what you want, use JavaScript Closures. A closure is a function, which uses the scope in which it was declared when invoked. It is not the scope in which it was invoked.ExampleLet’s take your example and this is how you can achieve your task. Here, innerDisplay() is a JavaScript closure.Var myFunction = (function () {    function display() {       // 5    };    function innerDisplay (a) {       if (/* some condition */ ) {          // 1          // 2          display();       }else {          // 3          // 4          display();       }    }    return innerDisplay; })();

Use Arrow Functions as Methods in JavaScript

Swarali Sree
Updated on 07-Jan-2020 08:03:40

225 Views

Fat arrow function as the name suggests helps in decreasing line of code. The syntax => shows fat arrow. This also avoids you to write the keyword “function” repeatedly. Arrow functions are generally used for a non-method function. Let’s see how to use arrow functions used as methods:ExampleYou can try to run the following code to implement arrow functions used as methodsLive Demo                    'use strict';          var ob1 = {             val1: 75,             val2: 100,             x: () => document.write(this.val1, this),             y: function() {                document.write(""+this.val1, this);             },             z: function() {                document.write(""+this.val2, this);             },          }          ob1.x();          ob1.y();          ob1.z();          

Where are JavaScript Variables Stored

Samual Sam
Updated on 07-Jan-2020 06:58:27

2K+ Views

Like many other programming languages, JavaScript has variables. Variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container.JavaScript variables get stored in the memory of the browser process. The following ways can work for storing variables:The variables which you declare in your JavaScript code gets saved in the memory of the browser process.Cookies can also store variables, they are often saved on your hard disk;

Windows Registry Access Using Python Winreg

Pradeep Elance
Updated on 07-Jan-2020 06:53:49

6K+ Views

As a versatile language and also availability of very large number of user supported modules, we find that python is also good at OS level programming. In this article we will see how python can access the registry of a windows operating system.We need to import the module named winreg into the python environment.In the below example we use the winreg module to first connect to the registry using the ConnectRegistry function and then access the registry using OpenKey function. Finally we design a for loop to print the result of the keys accessed.Exampleimport winreg #connecting to key in registry ... Read More

Filter Negative Values From Given Dictionary in Python

Pradeep Elance
Updated on 07-Jan-2020 06:48:05

557 Views

As part of data analysis, we will come across scenarios to remove the negative values form a dictionary. For this we have to loop through each of the elements in the dictionary and use a condition to check the value. Below two approaches can be implemented to achieve this.Using for loopW simply loop through the elements of the list using a for loop. In every iteration we use the items function to compare the value of the element with the 0 for checking negative value.Example Live Demodict_1 = {'x':10, 'y':20, 'z':-30, 'p':-0.5, 'q':50} print ("Given Dictionary :", str(dict_1)) final_res_1 ... Read More

Filter Even Values From a List in Python

Pradeep Elance
Updated on 07-Jan-2020 06:46:30

1K+ Views

As part of data analysis require to filter out values from a list meeting certain criteria. In this article we'll see how to filter out only the even values from a list.We have to go through each element of the list and divide it with 2 to check for the remainder. If the remainder is zero then we consider it as an even number. After fetching these even numbers from a list we will put a condition to create a new list which excludes this even numbers. That new list is the result of the filtering condition we applied.Using for ... Read More

Multiply Two Matrices in Single Line Using NumPy in Python

Pradeep Elance
Updated on 07-Jan-2020 06:41:16

763 Views

Matrix multiplication is a lengthy process where each element from each row and column of the matrixes are to be multiplied and added in a certain way. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result matrix has the number of rows of the first and the number of columns of the second matrix.For smaller matrices we may design nested for loops and find the result. For bigger matrices we need some built in functionality in python to tackle this. We will see both ... Read More

All Possible Permutations of N Lists in Python

Pradeep Elance
Updated on 07-Jan-2020 06:34:45

1K+ Views

If we have two lists and we need to combine each element of the first element with each element of the second list, then we have the below approaches.Using For LoopIn this straight forward approach we create a list of lists containing the permutation of elements from each list. we design a for loop within another for loop. The inner for loop refers to the second list and Outer follow refers to the first list.Example Live DemoA = [5, 8] B = [10, 15, 20] print ("The given lists : ", A, B) permutations = [[m, n] for m in ... Read More

Convert Lowercase Characters to Uppercase Based on Co-Prime ASCII Value in C++

Ayush Gupta
Updated on 06-Jan-2020 12:24:57

155 Views

In this tutorial, we will be discussing a program to convert all lowercase characters to uppercase whose ASCII value is co-prime with k.For this we will be provided with a string and an integer value k. Our task is to traverse through the given string and change to uppercase all those characters whose ASCII value is co-prime with the given integer k.Example Live Demo#include using namespace std; //modifying the given string void convert_string(string s, int k){    int l = s.length();    for (int i = 0; i < l; i++) {       int ascii = (int)s[i];   ... Read More

Advertisements