Home Web Front-end JS Tutorial Study notes written by someone when he first learned javascript_javascript skills

Study notes written by someone when he first learned javascript_javascript skills

May 16, 2016 pm 06:12 PM
javascript study notes

Copy code The code is as follows:

/*
* JavaScript对象就是一组属性(方法)的集合
* 在该语言中如果变量名或方法名不符合声明规范,
* 则一定得用方括号“ [] ”引用它
*
*/
/**
* <1.>该语句声明了一个class1类,class1相当于构造方法,又叫构造器
* 也可说声明了一个class1方法
*/
function class1(){
this.name="xjl"; //给对象添加属性
this.say= function(){alert("大家好!");}; //给对象添加方法
};
/**
* <2.>创建实例用 new 关键字,new 操作符不仅对内部类有效,对用户定义的类也是同样的用法
* 每个对象可以看作是多个属性(方法)的集合,即 对象名.属性(方法)名 或 对象名[“属性(方法)名”]
* 方括号 '[]' 适合不确定具体要引用哪个属性(方法)的场合
*/
var a = new class1();
//alert(typeof(a)); //typeof(a) 返回a的类型
//alert(a.name); //每个对象可以看作是多个属性(方法)的集合,
//alert(a['name']); //用方括号([])引用对象的属性和方法
//下拉框对象名[下拉框对象.value] 即可获得用户所选的值 也可用 eval(“下拉框对象名.” 下拉框对象.value);
//a.say(); //调用对象的方法
//var arr=new Array();
//arr['push']('abc'); //为数组添加一个元素,其中的 push 为内置的属性
//arr['push']('1234'); //为数组添加一个元素
//alert(arr);
/**
* <二.>动态添加、修改、删除对象的属性和方法
*
*/
var obj = new Object();
//添加属性……其中的属性名可任意取
obj.name="徐建龙";
obj.sex = '男';
obj['my name'] = "xujianlong"; //使用方括号 “ [] ”可以使用非标识符字符串作为属性名称
//添加方法……方法名也可任意取,也可传参数
obj.alert = function(a){
alert(a "你好!");
}
//修改属性,就是把属性的值改成别的内容
obj.name = "张三";
obj['name'] = 'anme';
//删除属性,就是把属性的值改成 undefined 或 null
obj.name = 'undefined';
/**
* <三>使用大括号({})语法创建无类型对象
*/
//在大括号中方属性和方法,属性与属性用逗号隔开,属性与值之间用冒号隔开
var ob = {
name:"123",
say:function(){alert("123")} //最后一个属性或方法不用逗号
}
//也可用如下方法定义对象的属性和方法
var ob1 = {"name":'123','say':function(){alert("abcd");}};
/**
*<四>prototype原型对象
* 所有的函数(function)对应的类是(Function)
* prototype 实际上就是表示了一个类的成员的集合。
* *当通过new 来获取一个类的对象时,prototype 对象的成员都会成为实例化对象的成员。
*/
function class2(){ //创建一个对象
}
var ob2 = new class2();
class2.prototype.me = function(){alert("123");} //在prototype的前面是,你所创建的类名
class2.prototype.name = "123"; //
/**
* 函数对象和其他内部对象的关系
*/
//typeof(new Function()),typeof(Function),typeof(Array),typeof(Object) 返回字符串“function”这些参数称之为构造器
//typeof(new Date()),typeof(new Array()),typeof(new Object()) 返回字符串“object”
/**
* 传递给函数的隐含参数:arguments,它具有数组的特点,但它不是数组,可用下标来访问它
*/
//arguments 中包含了一个参数 callee, 它表示对 函数对象本身的引用,如下:
var sum=function(n){
if(1==n)
return 1;
else
return n arguments.callee(n-1);
}
//该语句表示声明一个 namespace1 的命名空间 ,如下:
var namespace1 = new Object();
namespace1.class1 = function(){alert("123");};
var obj1=new namespace1.class1(); //页面加载时就执行
/**
* 使用prototype 对象定义类成员
*/
//在创建实例的语句之后使用函数的prototype属性给类定义新成员,只会对后面创建的对象有效
//在prototype中的constructor()方法 ,相当于构造方法
function class1(){
//alert('adf');
}
//class1.prototype.constructor(); //页面加载时就执行
//用prototype 定义的简化
class1.prototype={
//放入一些属性或方法
//多个属性或方法是用逗号(,)隔开
}
//如下代码即是 静态方法和属性
class1.name="abc";
class1.say = function(){/*codes*/}
//利用反射机制,可以改变 element 中指定的样式,而其他样式不会改变,得到了所要的结果,例如:
function setStyle(_style){
//得到要改变样式的界面对象
var element=getElement();
for(var p in _style){
element.style[p]=_style[p];
}
}
//可以通过拷贝一个类的 prototype 到另外一个类来实现继承,但有缺陷。例如:
// function class4(){}
//
// function class2(){
//
//
// class2.prototype=class4.prototype; //实现的继承
// class2.prototype.f = function(){alert("a");}
//
//当对class2 进行prototype 的改变时,class4 的prototype 也随之改变
// instanceof 操作符来判断一个对象是否是某个类的实例, 例如:
var a = new class2();
a instanceof class2; //返回一个 bool ,如果 a 的class2 中的继承类,则也是 true
//一种更好的继承
for(var p in class1.prototype){
class2.prototype[p]=class1.prototype[p];
}
class2.prototype.ma=function(){
alert(123);
}
//当对class2 进行prototype 的改变时,class4 的prototype 不会改变
/**
* prototype-1.3.1框架中的类继承实现机制
*/
//-------------------------------------------------------------------------------------------
//该语句为每个对象天加一个extend 方法,代码如下;
Object.extend = function(destination, source) {
for (property in source) {
destination[property] = source[property]; //将source中所有的属性或方法都赋值给destination
}
return destination;
}
//通过Object类为每个对象添加方法extend
Object.prototype.extend = function(object) {
return Object.extend.apply(this, [this, object]);
}
Object.extend.apply(this,[this,object]);
//class1继承与class2 ,好处是 通过new class2()相当于把class2的prototype的副本赋给class1
//在class1中的prototype的改变,不会影响class2中的prototyp
class1.prototype=(new class2()).extend({/*class1要增加的属性或方法*/});
/**
* 只做了一个声明而未实现的方法,具有虚函数的类就称之抽象类 ,抽象类是不能实例化的
*/
//里虚方法不需经过声明,而直接使用了。这些方法将在派生类中实现,例如:
function c1(){}
c2.prototype={
fun:function(){ this.fn();}//其中的fn方法未定义
}
function c2(){}
c1.prototype=(new c2()).extend({
fn:function(){var x = 1;}
});
//this.initialize.apply(this, arguments);该语句是把创建对象时的参数传给initialize方法
/***
* 在javascript中也可以用 try-catch-finally 语句用于捕获异常或错误信息
* 其中在catch(e)的小括号中的e 是必须的 e是一个名为error 的对象
* e=new Error(message)来创建这个对象,异常的描述被作为error 对象的一个属性message,
*/
//该代码演示了异常的抛出
function s(a,b){
try{
if(b==0)
throw new Error("除数不能为零!........");
else
alert(a/b)
}catch(e){
document.write(e.message);///通过message 获得Error中的实参
}
}
onlaod=s(1,0);
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

See all articles