javascript原型链

javascript 原型链

graph TB
A[对象]--->B[原型]

对象的创建

  1. 使用json创建

    var obj = {
        name:"",
        age:""
    }
    
  2. 使用工厂函数创建

    function createobj(){
        var obj = new Object();
        obj.name = "",
        obj.age = ""  
    }
    
  3. 构造函数

    image-20210503100629250

    
    

使用js实现单向链表

<script>
        // 使用js实现链表
        function Link() {
            this.head;
            this.size = 0;
            // 链表里面的节点
            function Node() {
                this.next;
                this.data;
            }
            // 添加头结点
            this.addhead = function(data) {
                let n = new Node();
                n.data = data;
                if (this.size == 0) {
                    this.head = n;
                    n.next = null;
                } else {
                    n.next = this.head;
                    this.head = n;
                }
                this.size++;
            }
            // 获取第几个节点
            this.getdata = function (a){
                let data = this.head;
                for(let i=1;i<a;i++){
                    data = data.next;
                }
                return data;
            }

            // 添加尾结点
            this.addtail = function(data){
                let n = new Node();
                n.data = data;
                if(this.size == 0){
                    this.head = n;
                    n.next = null;
                }else{
                    this.getdata(this.size).next = n;
                    n.next = null;
                }
                this.size++;
            }
            // 删除尾结点
            this.removetail = function(){
                // 获取最后一个节点的前一个节点,让前一个节点指向空
                this.getdata(this.size-1).next=null;
                this.size--;
            }
            // 删除头结点
            this.removehead = function(){
                this.head = this.head.next;
                this.size--;
            }
        }
        let link = new Link();
        link.addhead("ni");
        link.addhead("wo");
        link.addhead("ta");
        link.addtail("付立翔");

        // console.log(link.head.next);

    </scrip>

Js实现栈

  <script>
        // 实现栈
        function zhan(){
            this.head;
            this.size = 0;
            function Node(){
                this.data;
                this.next;
            }
            this.add = function(data){
                let n = new Node();
                n.data = data;
                if(this.size == 0){
                    this.head = n;
                    n.next = null;
                }else{
                    n.next = this.head;
                    this.head = n;
                }
                this.size++;
            }
            this.tan = function(){
                let he = this.head;
                this.head = this.head.next;
                return he;
            }
        }
    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值