Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts
Sunday, April 22, 2018
Tuesday, September 19, 2017
Class component in React
Functional component
Simple react component which renders a buttonconst Button= function () { return ( <button>Go</button> ); }; //Syntax to mount a component is ReactDOM.render //First argument is the component and the second argument is where the
//component needs to be rendered ReactDOM.render(<Button/>,mountNode);
Overview of React Native
- Provides native experience with less hassle and leverage existing skills using JavaScript and React
- UI consists of 100% native ios/android controls without performance issues
- A web like user interface which allows us to refresh app from the simulator
- Apps can be debugged using chrome developer tools
- We can style views decoratively similar to css
Thursday, August 31, 2017
Java script cheat sheet
Push to an array
outArray.push(arr2[k].charCodeAt()-64)
outArray.push(arr2[k].charCodeAt()-64)
/convert the character value to number //note we are subtracting 64 outArray.push(arr2[k].charCodeAt()-64)
Get first character from a string
function validatePalindrome(word) { //charArray = word.split; return word[0]; } //Function call console.log(validatePalindrome("BOB"));
Overview of React
- Is a JavaScript framework for building User interface
- It is not a framework
- It is considered as an view in MVC
- Virtual DOM gives React it's edge and makes it fast
Virtual DOM:
- Uses a virtual DOM to push only changes to the DOM instead of re-rendering everything
- V-Dom allows rendering on the server side and sending the final HTML to the browser (SEO friendly )
JSX;
Allows html in JavaScript which results in self contained components
Local storage in the browser
- The read-only local-storage property allows you to access a Storage object for the Document's origin;
- The stored data is saved across browser sessions.
- local-storage is similar to session-storage, except that while data stored in local-storage has no expiration time, data stored in session-storage gets cleared when the page session ends — that is, when the page is closed.
Use of Export and Require
Export
- The export statement is used when creating JavaScript modules to export functions, objects, or primitive values from the module so they can be used by other programs with the import statement
- Along with export we also need require or import
- Used to consume modules. It allows you to include modules into your programs. You can include built-in core Node.js modules, community-based modules (node_modules) and local modules
String to JSON object
- A common use of JSON is to exchange data to/from a web server.
- When receiving data from a web server, the data is always a string.
- Parse the data with JSON.parse(), and the data becomes a JavaScript object.
We would usually receive text from the server. We need to convert it into JavaScript object. In this scenario we use JSON.parse() function.
console.log("** Convert string into JSON object *****")
var jsonstring='{"firstName":"Prathap","lastName":"Kudupu"}' console.log("\n string-->",jsonstring); var obj=JSON.parse(jsonstring); console.log("\nJSON-->",obj);
JSON object to string
- A common use of JSON is to exchange data to/from a web server
- When sending data to a web server, the data has to be a string
- Convert a JavaScript object into a string with JSON.stringify()
console.log("*** Convert JSON object to a string"); var obj={ firstName:"Prathap", lastName:"Kudupu" } console.log('\n obj', obj) console.log('Convert JSON object to string',JSON.stringify(obj));
Output
Note: The difference between string and object is that the property name is the object is not a string
Split and join in JavaScript
Split a phrase into words
This function takes a given phrase and splits based on space into a new array
ex : It would take "Prathap Kudupu" it would add it to an array with prathap and kudupu
Edit in plnkr
console.clear(); console.log("**** Split a phrase *****"); function splitPhrase(str){ console.log(str); return str.split(' '); } console.log(splitPhrase('Prathap Kudupu'));
Difference between let and var
The difference is scoping.
var
is scoped to the nearest function block and let
is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.
Also, variables declared with
let
are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.
They are very similar when used like this outside a function block.
let me = 'go'; // globally scoped
var i = 'able'; // globally scoped
However, global variables defined with
let
will not be added as properties on the global window
object like those defined with var
.console.log(window.me); // undefined
console.log(window.i); // 'able'
Saturday, June 3, 2017
Client side PDF generation
We can generate PDF in JavaScript using jsPDF library.
Edit in plkr
Steps involved
- Use CDN reference to jsPDF
- Open the document
- Pass the Json Object to the document
- Save the file
Thursday, May 4, 2017
Wednesday, May 3, 2017
Tuesday, May 2, 2017
Promises in JavaScript
Promises
Promises in JavaScript are like promises in real life- A promise can only succeed or fail once
- It cannot succeed or fail twice, neither can it switch from success to failure or vice versa
- If a promise has succeeded or failed, we later add a success/failure callback. The correct callback will be called, even though the event took place earlier
Closures in JavaScript
A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.
Subscribe to:
Posts (Atom)
Labels
- Algorithms (52)
- Apache Kafka (7)
- Apache Spark (21)
- Architecture (8)
- Arrays (23)
- Big Data (98)
- Cloud services (6)
- Cognitive technologies (12)
- Data Analytics (3)
- Data Science (6)
- Design (1)
- Hadoop (26)
- Hive (11)
- Java (2)
- JavaScript (65)
- JavaScript Run-time (12)
- Machine learning (11)
- Maths (6)
- MySQL (1)
- Networking (3)
- No SQL (2)
- Node (20)
- Python (28)
- SQL (40)
- Security (4)
- Spark Grpahx (1)
- Spark MLlib (1)
- Spark Sql (3)
- Spark Streaming (4)
- Sqoop (2)
- Strings (13)
- devOps (1)
- mongoDb (2)
- ssis (3)