首页 > 代码库 > javascript学习:闭包和prototype原型使用基础

javascript学习:闭包和prototype原型使用基础

闭包

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, "59589231@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原型使用基础