Table of Contents
一.摘要
二.前言
三.Javascript面向对象
创建对象
创建属性并赋值
访问属性
嵌套属性
使用索引
JSON 格式语法
静态方法与实例方法
四.全局对象是window属性
五.函数究竟是什么
六."this"究竟是什么
七.javascript中的闭包
八.总结
Home Web Front-end JS Tutorial jQuery Theatrical Edition What you must know about javascript_jquery

jQuery Theatrical Edition What you must know about javascript_jquery

May 16, 2016 pm 06:52 PM
jquery Theater version

一.摘要

本文是jQuery系列教程的剧场版, 即和jQuery这条主线无关, 主要介绍大家平时会忽略的一些javascript细节.  适合希望巩固javascript理论知识和基础知识的开发人员阅读.

 

二.前言

最近面试过一些人, 发现即使经验丰富的开发人员, 对于一些基础的理论和细节也常常会模糊. 写本文是因为就我自己而言第一次学习下面的内容时发现自己确实有所收获和感悟.  其实我们容易忽视的javascript的细节还有更多, 本文仅是冰山一角. 希望大家都能通过本文有所斩获.

 

三.Javascript面向对象

Javascript是一门面向对象的语言,  虽然很多书上都有讲解,但还是有很多初级开发者不了解. 

创建对象

ps: 以前写过一篇详细的创建对象的文章(原型方法, 工厂方法等)但是找不到了, 回头如果还能找到我再添加进来.下面仅仅简单介绍.

在C#里我们使用new关键字创建对象, 在javascript中也可以使用new关键字:

<SPAN class=kwrd>var</SPAN> objectA = <SPAN class=kwrd>new</SPAN> Object();
Copy after login

但是实际上"new"可以省略:

<SPAN class=kwrd>var</SPAN> objectA = Object();
Copy after login


但是我建议为了保持语法一直, 总是带着new关键字声明一个对象.

创建属性并赋值

在javascript中属性不需要声明, 在赋值时即自动创建:

objectA.name = <SPAN class=str>"my name"</SPAN>;
Copy after login

访问属性

一般我们使用"."来分层次的访问对象的属性:

alert(objectA.name);
Copy after login

嵌套属性

对象的属性同样可以是任何javascript对象:

<SPAN class=kwrd>var</SPAN> objectB = objectA;
objectB.other = objectA;
<SPAN class=rem>//此时下面三个值相当, 并且改变其中任何一个值其余两个值都改变</SPAN>
Copy after login
<SPAN class=rem></SPAN>objectA.name;
objectB.name;
objectB.other.name;
Copy after login

使用索引

如果objectA上有一个属性名称为"school.college", 那么我们没法通过"."访问,因为"objectA.school.college"语句是指寻找objectA的school属性对象的college属性.

这种情况我们需要通过索引设置和访问属性:

     objectA[<SPAN class=str>"school.college"</SPAN>] = <SPAN class=str>"BITI"</SPAN>;
     alert(objectA[<SPAN class=str>"school.college"</SPAN>]);
Copy after login

下面几个语句是等效的:

    objectA[<SPAN class=str>"school.college"</SPAN>] = <SPAN class=str>"BITI"</SPAN>;
    <SPAN class=kwrd>var</SPAN> key = <SPAN class=str>"school.college"</SPAN>
    alert(objectA[<SPAN class=str>"school.college"</SPAN>]);
    alert(objectA[<SPAN class=str>"school"</SPAN> + <SPAN class=str>"."</SPAN> + <SPAN class=str>"college"</SPAN>]);    
    alert(objectA[key]);
Copy after login

JSON 格式语法

JSON是指Javascript Object Notation, 即Javascript对象表示法.

我们可以用下面的语句声明一个对象,同时创建属性:

    <SPAN class=rem>//JSON</SPAN>
    <SPAN class=kwrd>var</SPAN> objectA = {
      name: <SPAN class=str>"myName"</SPAN>,
      age: 19,
      school:
      {
        college: <SPAN class=str>"大学"</SPAN>,
        <SPAN class=str>"high school"</SPAN>: <SPAN class=str>"高中"</SPAN> 
      },
      like:[<SPAN class=str>"睡觉"</SPAN>,<SPAN class=str>"C#"</SPAN>,<SPAN class=str>"还是睡觉"</SPAN>]
    }
Copy after login

JSON的语法格式是使用"{"和"}"表示一个对象, 使用"属性名称:值"的格式来创建属性, 多个属性用","隔开.

上例中school属性又是一个对象. like属性是一个数组. 使用JSON格式的字符串创建完对象后, 就可以用"."或者索引的形式访问属性:

objectA.school[<SPAN class=str>"high school"</SPAN>];
objectA.like[1];
Copy after login

静态方法与实例方法

静态方法是指不需要声明类的实例就可以使用的方法.

实例方法是指必须要先使用"new"关键字声明一个类的实例, 然后才可以通过此实例访问的方法.

    <SPAN class=kwrd>function</SPAN> staticClass() { }; <SPAN class=rem>//声明一个类</SPAN>
    staticClass.staticMethod = <SPAN class=kwrd>function</SPAN>() { alert(<SPAN class=str>"static method"</SPAN>) }; <SPAN class=rem>//创建一个静态方法</SPAN>
    staticClass.prototype.instanceMethod = <SPAN class=kwrd>function</SPAN>() { <SPAN class=str>"instance method"</SPAN> }; <SPAN class=rem>//创建一个实例方法</SPAN>
   
Copy after login

上面首先声明了一个类staticClass, 接着为其添加了一个静态方法staticMethod 和一个动态方法instanceMethod. 区别就在于添加动态方法要使用prototype原型属性.

对于静态方法可以直接调用:

staticClass.staticMethod();
Copy after login

但是动态方法不能直接调用:

staticClass.instanceMethod(); //语句错误, 无法运行.
Copy after login

需要首先实例化后才能调用:

    <SPAN class=kwrd>var</SPAN> instance = <SPAN class=kwrd>new</SPAN> staticClass();<SPAN class=rem>//首先实例化</SPAN>
    instance.instanceMethod(); //在实例上可以调用实例方法
Copy after login

四.全局对象是window属性


通常我们在

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

In-depth analysis: jQuery's advantages and disadvantages In-depth analysis: jQuery's advantages and disadvantages Feb 27, 2024 pm 05:18 PM

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

See all articles