Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 特定原型上的对象原型_Javascript_Oop_Object_Prototype - Fatal编程技术网

Javascript 特定原型上的对象原型

Javascript 特定原型上的对象原型,javascript,oop,object,prototype,Javascript,Oop,Object,Prototype,如何仅为特定对象类型扩展对象类型?我不希望与使用object.color()的另一个库发生冲突。 目前,我正在使用: function random_int(start,end) { return Math.floor(Math.random()*(end-start+1)+start); } var balls = function balls (context) { this.canvas = context.canvas; this.context = contex

如何仅为特定对象类型扩展对象类型?我不希望与使用object.color()的另一个库发生冲突。 目前,我正在使用:

function random_int(start,end) {
    return Math.floor(Math.random()*(end-start+1)+start);
}
var balls = function balls (context) {
    this.canvas = context.canvas;
    this.context = context;
}
Object.prototype.ball = function ball() { }
balls.prototype.add = function () {
  this.context.fillStyle = '#ABC123';
  this.radius = 15;
  this.x = random_int(this.radius+1,this.canvas.width-this.radius-1);
  this.y = random_int(this.radius+1,this.canvas.height-this.radius-1);
  return this;
}
Object.prototype.draw = function () {
  this.context.beginPath();
  this.context.arc(this.x, this.y, this.radius, 0, (Math.PI/180)*360, false);
  this.context.fill();
  this.context.closePath();
}
balls.prototype.redraw = function () {
    this.context.save();
    this.context.setTransform(1, 0, 0, 1, 0, 0);
    this.context.clearRect(0, 0, canvas.width, canvas.height);
    this.context.restore();
    this.draw();
}
Object.prototype.color = function (col) {
    this.context.fillStyle = col;
}
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.canvas.width  = window.innerWidth*0.9;
context.canvas.height = window.innerHeight*0.9;
var Balls = new balls(context);
for (var i = 0; i<10;i++) {
    var b = Balls.add();
    b.color("#"+((1<<24)*Math.random()|0).toString(16));
    b.draw();
}
function random\u int(开始、结束){
返回Math.floor(Math.random()*(结束-开始+1)+开始);
}
变量球=函数球(上下文){
this.canvas=context.canvas;
this.context=上下文;
}
Object.prototype.ball=函数ball(){}
balls.prototype.add=函数(){
this.context.fillStyle='#ABC123';
这个半径=15;
this.x=random_int(this.radius+1,this.canvas.width this.radius-1);
this.y=random_int(this.radius+1,this.canvas.height this.radius-1);
归还这个;
}
Object.prototype.draw=函数(){
this.context.beginPath();
this.context.arc(this.x,this.y,this.radius,0,(Math.PI/180)*360,false);
this.context.fill();
this.context.closePath();
}
balls.prototype.redraw=函数(){
this.context.save();
this.context.setTransform(1,0,0,1,0,0);
this.context.clearRect(0,0,canvas.width,canvas.height);
this.context.restore();
这个.draw();
}
Object.prototype.color=函数(col){
this.context.fillStyle=col;
}
var canvas=document.getElementById(“canvas”);
var context=canvas.getContext(“2d”);
context.canvas.width=window.innerWidth*0.9;
context.canvas.height=window.innerHeight*0.9;
var Balls=新球(上下文);

对于(var i=0;i如果您不想冲突,为什么不使用自己的作用域?为什么需要扩展对象

$(document).ready(function () {
    function random_int(start,end) {
        return Math.floor(Math.random()*(end-start+1)+start);
    }
    var balls = function balls (context) {
        this.canvas = context.canvas;
        this.context = context;
    
    this.ball = function () { return this; }
    this.add = function () {
      this.context.fillStyle = '#ABC123';
      this.radius = 15;
      this.x = random_int(this.radius+1,this.canvas.width-this.radius-1);
      this.y = random_int(this.radius+1,this.canvas.height-this.radius-1);
      return this;
    }
    this.draw = function () {
      this.context.beginPath();
      this.context.arc(this.x, this.y, this.radius, 0, (Math.PI/180)*360, false);
      this.context.fill();
      this.context.closePath();
    }
    this.redraw = function () {
        this.context.save();
        this.context.setTransform(1, 0, 0, 1, 0, 0);
        this.context.clearRect(0, 0, canvas.width, canvas.height);
        this.context.restore();
        this.draw();
    }
    this.color = function (col) {
        this.context.fillStyle = col;
    }
    }
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");
    context.canvas.width  = window.innerWidth*0.9;
    context.canvas.height = window.innerHeight*0.9;
    var Balls = new balls(context);
    for (var i = 0; i<10;i++) {
        var b = Balls.add();
        b.color("#"+((1<<24)*Math.random()|0).toString(16));
        b.draw();
    }
});
$(文档).ready(函数(){
函数random_int(开始、结束){
返回Math.floor(Math.random()*(结束-开始+1)+开始);
    }
变量球=函数球(上下文){
this.canvas=context.canvas;
this.context=上下文;
    
this.ball=函数(){返回this;}
this.add=函数(){
this.context.fillStyle='#ABC123';
这个半径=15;
this.x=random_int(this.radius+1,this.canvas.width this.radius-1);
this.y=random_int(this.radius+1,this.canvas.height this.radius-1);
归还这个;
    }
this.draw=函数(){
this.context.beginPath();
this.context.arc(this.x,this.y,this.radius,0,(Math.PI/180)*360,false);
this.context.fill();
this.context.closePath();
    }
this.redraw=函数(){
this.context.save();
this.context.setTransform(1,0,0,1,0,0);
this.context.clearRect(0,0,canvas.width,canvas.height);
this.context.restore();
这个.draw();
    }
this.color=函数(col){
this.context.fillStyle=col;
    }
    }
var canvas=document.getElementById(“canvas”);
var context=canvas.getContext(“2d”);
context.canvas.width=window.innerWidth*0.9;
context.canvas.height=window.innerHeight*0.9;
var Balls=新球(上下文);
对于(var i=0;i最终这样做:

function random_int(start, end) {
    return Math.floor(Math.random() * (end - start + 1) + start);
}
var balls = function balls(context) {
    this.context = context;
    this.add = function () {
        return new ball(context);
    }
}
Object.prototype.ball = function ball(context) {
                this.context = context;
                this.canvas = context.canvas;
    this.context.fillStyle = '#ABC123';
    this.radius = 15;
    this.x = random_int(this.radius + 1, this.canvas.width - this.radius - 1);
    this.y = random_int(this.radius + 1, this.canvas.height - this.radius - 1);
    this.draw = function () {
        this.context.beginPath();
        this.context.arc(this.x, this.y, this.radius, 0, (Math.PI / 180) * 360, false);
        this.context.fill();
        this.context.closePath();
    }
    this.redraw = function () {
        this.context.save();
        this.context.setTransform(1, 0, 0, 1, 0, 0);
        this.context.clearRect(0, 0, canvas.width, canvas.height);
        this.context.restore();
        this.draw();
    }
    this.color = function (col) {
        this.context.fillStyle = col;
    }
    return this;
}
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.canvas.width = window.innerWidth * 0.9;
context.canvas.height = window.innerHeight * 0.9;
var Balls = new balls(context);
for (var i = 0; i < 10; i++) {
    var b = Balls.add();
    b.color("#" + ((1 << 24) * Math.random() | 0).toString(16));
    b.draw();
}
function random\u int(开始、结束){
返回Math.floor(Math.random()*(end-start+1)+start);
}
变量球=函数球(上下文){
this.context=上下文;
this.add=函数(){
返回新球(上下文);
}
}
Object.prototype.ball=函数球(上下文){
this.context=上下文;
this.canvas=context.canvas;
this.context.fillStyle='#ABC123';
这个半径=15;
this.x=random_int(this.radius+1,this.canvas.width-this.radius-1);
this.y=random_int(this.radius+1,this.canvas.height-this.radius-1);
this.draw=函数(){
this.context.beginPath();
this.context.arc(this.x,this.y,this.radius,0,(Math.PI/180)*360,false);
this.context.fill();
this.context.closePath();
}
this.redraw=函数(){
this.context.save();
this.context.setTransform(1,0,0,1,0,0);
this.context.clearRect(0,0,canvas.width,canvas.height);
this.context.restore();
这个.draw();
}
this.color=函数(col){
this.context.fillStyle=col;
}
归还这个;
}
var canvas=document.getElementById(“canvas”);
var context=canvas.getContext(“2d”);
context.canvas.width=window.innerWidth*0.9;
context.canvas.height=window.innerHeight*0.9;
var Balls=新球(上下文);
对于(变量i=0;i<10;i++){
var b=Balls.add();

b、 颜色(#“+”((1)在您的示例中加入更多内容,看看真正的问题,javascript没有类,顺便说一句,我真的不建议扩展Objects。这是覆盖这个,我需要将它分开,这样我可以有不同的b,如果这让senseI不明白您的意思,
this
是私有特殊变量,我覆盖了它?向y添加更多代码我们的示例。你的作用域很古怪。不管怎样,我现在已经解决了。看看我的答案。你听说过私有属性吗?你为什么需要扩展基本的
对象
?它是JS中所有东西(包括字符串、数字等)的基类。所以你的代码会变得非常混乱。定义你自己的
MyObject
(或一些独特的东西)并将其用作其他东西(如Ball)的基础。将所有内容放在一个大对象中创建名称空间模拟也是一种很好的做法。