Basic JavaScript 4 -- function events and JSON

The first official account of this article: ELO's cabin, welcome to pay attention and view more content!!!

function

The code block stores a piece of code for processing a single task, and then calls it with a short command whenever necessary
JavaScript has many built-in functions. Browser functions are not part of the core JavaScript language, but are defined as part of the browser API

method

Functions are called part of object methods, and built-in browser functions are not functions -- they are methods

Custom function

Functions defined according to their own requirements

Call function

Call followed by parentheses

function demoFunction() {
  alert('hi function!');
}

demoFunction()
Anonymous function

A function without a name is an anonymous function

function() {
  alert('no name');
}

Anonymous functions are typically used with event handlers

Function parameters
var demoText = 'I am a boy';
var newText = demoText.replace('boy', 'girl');

'boy' and 'girl' are the parameters of the replace function

Function return value

Refers to the value returned after the function is executed

var demoText = 'I am a string';
var demoString = myText.replace('string', 'demo');
console.log(demoString);

The above content saves the return value as the content of demoString variable

Use return value

Use the return keyword

function randomNumber(number) {
  return Math.floor(Math.random()*number);
}
event

Events are actions or events that occur in the system during programming. The system will generate or trigger certain signals when events occur, and will provide an automatic loading of certain actions. Each available event will have an event processor, which is sometimes called an event listener

object

An object is a collection of related data and methods. To create an object, you usually define initialization variables first

var person = {};

Enrich the object

var person = {
  name : ['Elo', 'Yiluo'],
  age : 19,
  gender : 'male',
  interests : ['games', 'shopping'],
  contact : 'Wechat search: ELO's Cabin',
  greeting: function() {
    alert('Hi! I\'m ' + this.name[0] + '.');
  }
};

Then call

person.name[0]
person.age
person.greeting()

View the result of the call

person.name[0]
"Elo"
person.age
19
person.greeting
ƒ () {
    alert('Hi! I\'m ' + this.name[0] + '.');
  }
person.contact;
"Wechat search: ELO's Cabin"
person.greeting()
undefined
Point representation

In the above example, dot notation is used to access the properties and methods of the object. The name of the object must be written first,

Bracket notation

Using bracket notation, objects map strings to values, such as

person['age']
person['name']['age']
Meaning of "this"

The keyword "this" points to the object of the current code runtime.

Prototype language

JavaScript is often described as a prototype based language. To be exact, these attributes and methods are defined on the prototype attribute on the constructor functions of the Object, rather than the Object instance itself

JSON

JSON is a data format based on JavaScript object syntax. JSON can exist as an object or string with the extension of json

JSON structure
demoObj = {
    "name":"Elo Yiluo",
    "zone":"Wechat search: ELO's Cabin",
    "sites": {
        "site1":"https://yiluotalk.com/",
        "site2":"https://yiluotalk.com/",
        "site3":"https://yiluotalk.com/"
    }
}

Welcome to [poke] and [like] below
Author: Yiluo
May you enjoy every day, just joy!

Tags: Python Java Javascript Programming

Posted by TutorMe on Sun, 15 May 2022 09:34:48 +0300