V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
acthtml
V2EX  ›  JavaScript

js prototype 中 js 指针的访问。

  •  
  •   acthtml · 2014-05-12 11:51:00 +08:00 · 2643 次点击
    这是一个创建于 3638 天前的主题,其中的信息可能已经有所发展或是发生改变。
    function Parent(name){
    this.name = name;
    return this.init;
    }

    Parent.prototype = {
    name : '',
    init : function(){
    return this;
    }
    }

    Parent.prototype.son = {}

    ======================
    在 Parent.prototype.son 的方法中,如何访问Parent.prototype.name这个属性。
    4 条回复    2014-05-12 13:54:12 +08:00
    jsonline
        1
    jsonline  
       2014-05-12 12:10:19 +08:00   ❤️ 1
    prototype 的属性可以看作是 Parent 的类成员。
    类成员(son)想访问另一个类成员(name)只能以类(Parent)开头了。
    jsonline
        2
    jsonline  
       2014-05-12 12:13:35 +08:00   ❤️ 1
    严格来说也不能算类成员……

    为什么要把一个对象放在 prototype 里呢?你的 use case 是什么?
    ldb1992
        3
    ldb1992  
       2014-05-12 12:45:51 +08:00   ❤️ 1
    当作静态方法访问,或者把son的原型指向parent的原型,好久没写js了,记不得了
    acthtml
        4
    acthtml  
    OP
       2014-05-12 13:54:12 +08:00
    感谢楼上几位的回复。

    我这种写法本身有弊端。结贴,换另一种写法。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   935 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:43 · PVG 03:43 · LAX 12:43 · JFK 15:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.