javascript中静态方法、实例方法、内部方法和原型的一点见解
1、静态方法的定义
var BaseClass = function() {}; // var BaseClass=new Function();
BaseClass.f1 = function(){//定义静态方法
alert(' This is a static method ');
}
BaseClass.f1();//This is a static method
var instance1 = new BaseClass();
instance1.f1();//instance1.f1 is not a function
由以上代码分析可知,静态方法不能被实例对象调用,再看以下代码
var BaseClass = new Function;
var Class2 = BaseClass;
BaseClass.f1 = function(){
alert("BaseClass ' s static method");
}
Class2.f2 = function(){
alert("Class2 ' s static method");
}
BaseClass.f1();//BaseClass ' s static method
BaseClass.f2();//Class2 ' s static method
Class2.f1();//BaseClass ' s static method
Class2.f2();//Class2 ' s static method
从运行结果来看,BaseClass和Class都有f1和f2静态方法,实际上这两个函数是一样的,指向的是同一块内容空间的引用,可以执行以下代码来验证
alert(BaseClass == Class2);//true
如果删除其中一个函数中的静态方法,则对应的另一个函数的静态方法也被删除,比如执行
delete Class2.f2;
同时也会删除BaseClass中的f2
2、实例方法的定义
这里是利用javascript对象原型引用prototype来实现的,看以下代码
var BaseClass = function() {};
BaseClass.prototype.method1 = function(){
alert(' This is a instance method ');
}
var instance1 = new BaseClass();
instance1.method1();//This is a instance method
method1即为通过prototype原型引用定义的实例方法,这里也可以在实例上直接定义方法(变量),看以下代码
var BaseClass = function() {};
var instance1 = new BaseClass();
instance1.method1 = function(){
alert(' This is a instance method too ');
}
instance1.method1();//This is a instance method too
下面介绍通过this指针来定义实例方法(变量),看以下代码
var BaseClass = function() {
this.method1 = function(){
alert(' Defined by the "this" instance method');
}
};
var instance1 = new BaseClass();
instance1.method1();//Defined by the "this" instance method
那么同时在实例上、原型引用上和“this”上定义了相同名字的实例方法后,实例会优先调用那一个呢?请看以下代码
var BaseClass = function() {
this.method1 = function(){
alert(' Defined by the "this" in the instance method');
}
};
var instance1 = new BaseClass();
instance1.method1 = function(){
alert(' Defined directly in the instance method');
}
BaseClass.prototype.method1 = function(){
alert(' Defined by the prototype instance method ');
}
instance1.method1();//Defined directly in the instance method
通过运行结果可以看出直接定义在实例上的变量的优先级要高于定义在“this”上的,而定义在“this”上的又高于 prototype定义的变量。即直接定义在实例上的变量会覆盖定义在“this”上和prototype定义的变量,定义在“this”上的会覆盖prototype定义的变量。
3、内部方法
var BaseClass = function() {
var method1 = function() {
alert("Internal method");
};
var method2 = function() {
alert("call Internal method");
method1();
};
this.method3 = function(){
method2();
}
};
var instance1 = new BaseClass();
instance1.method1();// 会报错,因为method1是BaseClass中定义的内部变量,作用域只有在内部可见(闭包)
instance1.method3();//会先后调用method2和method1
ref : http://blog.csdn.net/jerrysbest/article/details/6642003
javascript中静态方法、实例方法、内部方法和原型的一点见解的更多相关文章
- Java 中静态方法 实例方法 具体方法区别与联系
在查阅JDK文档时,经常会看到某个类的方法汇总,一般会以如下的格式列出来: 这几个标签对应的方法类型分别是什么意思呢? 1. Static Method,静态方法,可以在不创建类实例的情况下,访问 ...
- JavaScript文件中调用AngularJS内部方法或改变$scope变量
需要在其他JavaScript文件中调用AngularJS内部方法或改变$scope变量,同时还要保持双向数据绑定: 首先获取AngularJS application: 方法一:通过controll ...
- 深入理解JavaScript中创建对象模式的演变(原型)
深入理解JavaScript中创建对象模式的演变(原型) 创建对象的模式多种多样,但是各种模式又有怎样的利弊呢?有没有一种最为完美的模式呢?下面我将就以下几个方面来分析创建对象的几种模式: Objec ...
- JavaScript中Object.prototype.toString方法的原理
在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. ? 1 2 var arr = []; console.lo ...
- URL地址中中文乱码详解(javascript中encodeURI和decodeURI方法、java.net.URLDecoder.encode、java.net.URLDecoder.decode)
引言: 在Restful类的服务设计中,经常会碰到需要在URL地址中使用中文作为的参数的情况,这种情况下,一般都需要正确的设置和编码中文字符信息.乱码问题就此产生了,该如何解决呢?且听本文详细道来. ...
- 改变JavaScript中函数的内部this指向!
改变JavaScript中函数的内部this指向! 第一种方法 call call 可以 调用函数 + 改变函数内的this指向! var obj = { name: 'lvhang' } funct ...
- Jquery中$(document).ready()与传统JavaScript中的window.onload方法的区别(2016/8/3)
Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间 ...
- JavaScript中事件绑定的方法总结
最近收集了一些关于JavaScript绑定事件的方法,汇总了一下,不全面,但是,希望便于以后自己查看. JavaScript中绑定事件的方法主要有三种: 1 在DOM元素中直接绑定 2 JavaScr ...
- Javascript中alert</script>的方法
Javascript中alert</script>的方法: <%@ page language="java" import="java.util.*&q ...
随机推荐
- 用Jekyll在github上写博客——《搭建一个免费的,无限流量的Blog》的注脚
本来打算买域名,买空间,用wordpress写博客的.后来问了一个师兄,他说他是用github的空间,用Jekyll写博客,说很多人都这么做.于是我就研究了一下. 比较有价值的文章有这么几篇: htt ...
- 在使用sqlite时淌过的坑
以前一直用sqlite.net 1.0.66.0版本,在.net4下面程序写好了部署到目的地机器时winform程序总是出现缺少运行时的问题.有时装了运行时也还是出问题,后来发现是混合模式的问题,当时 ...
- JAVA多线程编程之生产者消费者模式
Java中有一个BlockingQueue可以用来充当堵塞队列,下面是一个桌面搜索的设计 package net.jcip.examples; import java.io.File; import ...
- 页面头部title、description、keywords标签的优化
页面头部优化<Head></Head>中间的区域中间的区域,我们称为网页的头部.在网页的头部中,通常存放一些介绍页面内容的信息,例如页面标题.描述及关键字等等.在头部优化中,除 ...
- HANA Studio中修改默认查询结果只显示1000行
- Entityframework 事务
Working with Transactions (EF6 Onwards) This document will describe using transactions in EF6 includ ...
- mysql提示:Illegal mix of collations for operation ‘UNION’
http://www.111cn.net/database/mysql/56096.htm show variables like "%char%"; show variables ...
- eclipse中对项目进行分类管理
我们在用Eclipse开发的时候通常会建很多类型的项目,如公司项目.自己项目.Demo等等,并且一个项目又可能有一个主项目和多个引用包,如果包所有的项目都放到一个workspace下面,则会引起混来, ...
- Maven进价:使用m2eclipse创建web项目
1.新建Maven项目 2.设置项目空间 3.选择maven-archetype-webapp 4.填写Maven坐标 Maven坐标:groupId:artifactId:packaging:ver ...
- c++中继承和java中继承的对比
java中: class Parent{ public void test(int a){ System.out.println("Parent:" + a); System.ou ...