1

I am new with Ethereum technology. I am studying web3js javascript library from the official documentation(https://github.com/ethereum/wiki/wiki/JavaScript-API#web3) and i don't understand the difference between web3 and Web3 object. Why can i not use directly web3 for example in this code (in my html file):

var web3istance= new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); ...

Why Web3 if the object is web3? (Sorry for my bad english)

3

If you are familiar with object oriented programming, you can think of Web3 as the class, and web3 as an instance of the class, i.e. an object. It is also possible to have several different instances of Web3:

web3_svr1 = new Web3(new Web3.providers.HttpProvider(ip_of_server1));
web3_svr2 = new Web3(new Web3.providers.HttpProvider(ip_of_server2));

You can also see that you can chose an arbitrary name for the instance of Web3, it does not have to be web3.

1

When you import web3 in a React project for example, like so:

import Web3 from 'web3';

What you have there is a constructor function and thats why importing it is done with a capital W.

Afterwards, you create a new instance of web3 like so:

const web3 = new Web3(window.web3.currentProvider);

So the above code is an instance of web3 and in this case I passed in a few things such as window which is a global variable because this example is a case where you may have Metamask which injects web3 onto the global variable and you want to get rid of it for your own copy. currentProvider has been configured to connect to the Rinkeby test network and has access to all accounts, public keys and so on.

So I am taking that and giving it to the local instance of web3.

Lastly, again if this was a React project, you would export it like so: export default web3;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.