Kselax.ru

Hacker Kselax – the best hacker in the world

Menu
  • Blog
  • Contacts
  • wp plugin generator
  • English
    • Русский
Menu

JavaScript sclerotic

Posted on 17 July, 201924 August, 2019 by admin

How to convert value to string

you have three ways Converting a value to string in JavaScript

  1. String(value)
  2. “” + value
  3. value.toString()

value.toString is the worst. It doesn’t always work. So don’t use it. I personally use String(value) or “” + value

 

How to use this (hard binding in javascript)

this is a binding that is defining in function invocation time. Where the function is invoke that environment variables will be binded to this
For instance in node.js we have such code. The this value define its invocation environment

index.js

1
2
3
4
5
6
7
function foo() {
  console.log(this.a)
}
 
var a = 10
 
foo() // undefined

we’ve got undefined because in there this is bind to the global scope and in our example var a = 10 related to the local scope of the file index.js. it seems like that. That’s why we got undefined, To get 10 we have to bind a to the global this without var typing a = 10 then we will get 10

Hard bind is a way where we bind functions to the object explicitly. Let’s look at the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function foo() {
  console.log(this.a)
}
 
let obj = {
  a: 10
}
 
var myF = function() {
  foo.call(obj)
}
 
myF() // 10
setTimeout(myF, 100) // 10

We call it “hard binding” no the myF function is hard bound to the obj. Everytime we call the myF it the this will have variables from of obj this

 

the end

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • bash (1)
  • English (9)
  • JavaScript (4)
  • node.js (22)
  • photoshop (1)
  • php (3)
  • React (9)
  • sclerotic (6)
  • Ubuntu (10)
  • Uncategorized (13)
  • Wordpress (1)

Tags

Ajax apache2 automation bash chrome-extension command line editor ejs email English English-grammar framework functions git graphql handlebars hybrid app installation javascript js linux newbie node.js node.js javascript nodemailer npm objects Performance php phpmyadmin playonlinux promise rabbitmq React react-router redis reverse-proxy session shell socket.io sublime text 3 time zones ubuntu unity webpack

Recent Comments

  • damien on How to install npm and nodejs the latest versions on ubuntu
  • Cam on How to install npm and nodejs the latest versions on ubuntu
  • Pierre on socket.io with apache as a reverse proxy on the CentOS
  • admin on How to use react-router with a few languages
  • admin on How to install npm and nodejs the latest versions on ubuntu
©2021 Kselax.ru Theme by ThemeGiant