JavaScript basics-custom objects

 
table of Contents

Everything in JavaScript is an object: strings, numbers, arrays, functions... In addition, JavaScript allows custom objects.

According to the JS object extension mechanism, users can customize JS objects, which is similar to the Java language. Corresponding to custom objects are JS standard objects, such as Date, Array, Math, etc.

type of data

Basic data types (stack memory) : number, string, null, Boolean, Underfind

Reference type (heap memory) : object 

Syntax for creating an object: create an object of obj

1. var obj=new object(); //constructor

2. var obj={}; //Use object literal

3. var fun3=function([formal parameter 1, formal parameter n]){

                 //Function body

};

parameter

When calling a function, the parser does not check the types of actual parameters; when calling a function, the parser does not check the number of actual parameters.

Redundant arguments do not participate in calculations

If the number of actual parameters is less than the number of formal parameters, the formal parameters without corresponding actual parameters will be underfind

There are two scopes in js

1. Global scope

         Written in the script tag, created when the page is opened, and destroyed when the page is closed

         The global scope has a window object,

         The variables created are all properties of the Window object

         The functions created are methods of the window object

2. Function scope

         The function scope is created when the function is called. After the function is executed, the scope is destroyed

         In the function, variables declared without var will become global variables

Declare in advance

1. The declaration of variables in advance

        Variables declared with the keyword var will be declared before all code is executed and will not be assigned

        Value will not be assigned until the line is executed.

2. The declaration of the function in advance

      Function created using the function declaration form function function name (){}

      It will be created before all the code is executed, so we can call the function before the function declaration

this pointer

The parser will pass an implicit parameter into the function every time it calls the function. The implicit parameter is this. Depending on how the function is called, this will point to different objects.

1. When called as a function, this is always window

2. When called in the form of a method, this is the object that calls the method

Prototype object prototype

For every function we create, the parser adds an attribute prototype to the function. This attribute corresponds to an object, which is what we call the prototype object

 The prototype object is a public area, all objects of the same type can access this prototype object

 We can uniformly set the public content in the object in the prototype object

 When creating a constructor, you can uniformly add the public properties and methods of the object to the prototype object of the constructor

function MyClass(name,age,gender){
	this.name=name;
	this.age=age;
	this.gender=gender;
	//原型函数sayHello写在外边
}

//向MyClass的原型中添加属性a,函数sayHello
    MyClass.prototype.a="123";
    MyClass.prototype.sayHello = function(){
	    alert("hello大家好我是"+this.name);
    };

var mc=new MyClass("猪八家",18,"男");
mc.sayHello();

Use in to check whether the object contains a certain property. If the prototype has this property, it also returns true

       console.log("age" in mc);

Use hasOwnProperty() to check that the object itself contains this property. Return true only if the object itself contains attributes

       console.log(mc.hasOwnProperty("age"));

If this blog is helpful to you, please remember to leave a message + like it.

Guess you like

Origin blog.csdn.net/promsing/article/details/110141359