闭包

function Person(name) {
this.Username = name;
var Userage = 18; //通过这种方法可以模拟私有成员
//类似于private成员
this.setAge = function (age) {
Userage = age;
}
//类似于public成员
this.getAge = function () {
return Userage;
}
}
var p1 = new Person("huahuah");
p1.setAge(100); alert(p1.getAge())
//------------------------------------------------- var x = 100;
//执行3 找到x定义
function f1() {
var y = 101;
//执行2,找到x未定,继续找
alert(y);
alert(x);
//整个return函数就是常说的闭包
//由此函数开始执行1,找不到x
//闭包靠的是作用域链作用的,必须一层一层释放
return function () {
var y = 99;
alert(x);
alert(y);
//向上找
}
}
function f1() {
var funs = new Array();
//2:找到i,但是i已经循环遍历了i=10
for (var i = 0; i < 10; i++) {
//1:先执行闭包内,找不到i,搜索外层
funs[i] = function () {
alert(i);
}
}
//3:返回i=10
return funs;
}
//4:声明myfuns=f1()
var myfuns = f1(); for (var n = 0; n < myfuns.length; n++) {
//5:因为f1的长度=10,所以n的长度也=10,循环遍历弹出n的值
myfuns[n]();
}

原型:

//prototype原型
function Person(name, age, email) {
this.UserName = name;
this.UserAge = age;
this.UserEmail = email;
this.sayHi=function(){
alert('你好,我的名字是' + this.UserName + '今年' + this.UserAge + '岁了' + '我的联系邮箱是' + this.UserEmail); }
this.sayHellp= function () {
alert()
}
}
//通过构造函数创建的对象 都是完全独立的对象 对象与对象之间是没有关系的,类似c#中的对象
var p1 = new Person("黄", "18", "595892312@qq.com");
p1.sayHi();
var p2 = new Person("huang", "23", "123@163.com");
alert('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
p2.sayHi();
p1.UserName = "zhen";
p1.UserAge = 15;
p1.UserEmail = "854658496@sina.com";
p1.sayHi(); ///////////////////////////原型2
//定义构造函数
function Person(name, age, email) {
this.UserName = name;
this.UserAge = age;
this.UserEmail = email;
}
//_proto_
//prototype是person对象的原型对象
//在person的原型对象中加一个sayHi()方法
Person.prototype.sayHi = function () {
alert("My name is" + this.UserName + "," + this.UserAge + "old,My Email is" + this.UserEmail);
}
//通过构造函数创建函数对象
var p1 = new Person("susan", 18, "suan@gogle.com");
p1.sayHi();
var p2 = new Person("黄", 23, "5231@qq.com");
p2.sayHi(); ////////////////通过原型实现扩展方法
//给字符串对象原型添加haha方法
String.prototype.haha=function (){
return this+"☆";
};
//创建字符串对象
var msg = '56465455645';
msg = msg.haha();
alert(msg); ///通过原型prototype实现继承
//js中没有类的概念,继承是通过对象和对象来实现的
function Person(name,age,email) {
this.Username = name;
this.Userage = age;
this.Useremail = email;
}
Person.prototype.sayHi = function () {
alert("我的名字叫做" + this.Username + "今年" + this.Userage + "岁了,我的邮箱是:" + this.Useremail);
};
//student
function Student(sid) {
this.student_id = sid;
}
//继承 通过prototype=p1继承Person中的属性
Student.prototype = new Person("黄", 18, "huang@163.com");
var s1 = new Student('1564156165');
s1.Username = '李'; alert(s1.Username);

javascript学习:闭包和prototype原型使用基础的更多相关文章

  1. 【JavaScript 封装库】Prototype 原型版发布!

    /* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...

  2. javascript学习总结(一):基础知识。

    1 数据类型a.数据类型共有7种,字符串(string).数字(number).布尔(boolean).数组(array).对象(object).Null.Undefined. 其中布尔(逻辑)类型只 ...

  3. Javascript学习-闭包

    看的这篇 http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html 各种专业文献上的"闭包" ...

  4. javascript --学习闭包,自由变量

    闭包 下面是百度百科的解释: 闭包是指可以包含自由(未绑定到特定对象)变量的代码块:这些变量不是在这个代码块内或者任何全局上下文中定义的,而是在定义代码块的环境中定义(局部变量). 也就是说一个代码块 ...

  5. JavaScript oop proto与prototype原型图

    [_proto_与prototype] 1.prototype(函数的原型):函数才有prototype.prototype是一个对象,指向了当前构造函数的引用地址. 2._proto_(对象的原型对 ...

  6. Javascript学习笔记--理解prototype

    prototype和closure是js中两个不好搞懂的概念,幸好网上有很多相关的文章,在网上查了一遍以后,总是是觉得有点理解了.今天先说说prototype. 之前一直被ajax in action ...

  7. javascript学习笔记(一):基础、输出、注释、引用、变量、数据类型

    javascript脚本必须位于<script></script>之间,<script>标签可以位于<head>中,也可以位于<body>中 ...

  8. JavaScript学习总结(三)——闭包、IIFE、原型、函数与对象

    一.闭包(Closure) 1.1.闭包相关的问题 请在页面中放10个div,每个div中放入字母a-j,当点击每一个div时显示索引号,如第1个div显示0,第10个显示9:方法:找到所有的div, ...

  9. 深入理解javascript原型和闭包(3)——prototype原型 (转载)

    深入理解javascript原型和闭包(3)——prototype原型   既typeof之后的另一位老朋友! prototype也是我们的老朋友,即使不了解的人,也应该都听过它的大名.如果它还是您的 ...

随机推荐

  1. hexo的jacman主题配置

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 这是在我搭建博客时用的主题,这个主题时基于pacman修改的,同时我也是借助于wuchong同时他还在一直更新.一下时我的一些基本配置: 相关文章 ...

  2. django 部署到Ubuntu-apache2遇到的问题汇总

    1.x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’ Turns out ...

  3. HTTP上下文表单内容转为实体对象

    using ServiceStack.Web; using System; using System.Collections.Generic; using System.Linq; using Sys ...

  4. thinkphp3.2.3 批量包含文件

    自己瞎写的...凑合看吧...核心就是用正则 表达式 或者 字符串 str_replace 进行 替换....... /** * 批量包含---,不能递归包含!!! 请不要在目标目录 包含 文件夹,因 ...

  5. 如何写javascript代码隐藏和显示这个div

    如何写javascript代码隐藏和显示这个div 浏览次数:82次悬赏分:10 | 解决时间:2011-4-21 14:41 | 提问者:hade_girl <div id="div ...

  6. Linux 安装 python3

    1. 安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline- ...

  7. codeforces 1101F Trucks and Cities 区间dp+单调优化 好题

    题目传送门 题意简述:(来自洛谷) 有n个城市坐落在一条数轴上,第ii个城市位于位置ai​. 城市之间有m辆卡车穿行.每辆卡车有四个参数:si​为起点编号,fi​为终点编号,ci​表示每行驶1个单位长 ...

  8. 将M个客服随机分配给N个客户

    class AllocUser { //客户多于客服 public static void Test() { var customers = new List<Customer>() { ...

  9. 剑指offer——面试题15.2:判断两个整数m和n的二进制中相差多少位

    #include"iostream" using namespace std; int CountDifferentBit(int m,int n) { ,diff=m^n; wh ...

  10. 关闭Eclipse按空格和等号键自动补全内容

    当我们在Eclipse中设置按下任何字母都弹出候选菜单后(默认只有再按"."后才会后输入的弹出菜单), 当是设置完后每次在输入变量的时候Eclipse就会自动给我们补全变量,就是在 ...