为什么“这个”变量不是期望值? [英] Why is the 'this' variable not the expected value?

查看:101
本文介绍了为什么“这个”变量不是期望值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下代码的问题。在具有console.log的行上,'this'变量应该包含Object Chart,而是包含path.line。因此,对xscale的引用是未定义的。为什么我得到这个?此错误发生在Chrome和Firefox下。



谢谢。

  <!DOCTYPE html> 
< html>
< head>
< title> test< / title>
< meta charset =utf-8>
< style>
body {
font:10px sans-serif;
}

.line {
fill:none;
stroke:steelblue;
stroke-width:3.0px;
}
< / style>
< / head>
< body>
< script src =d3.v2.js>< / script>
< script>
var kev_vs_rho = [{
values:[{x:0.01,y:0.2058},{x:0.03,y:0.2039},{x:0.05,y:0.2020}]},{$ b:b值:[{x:0.01,y:1.6468},{x:0.03,y:1.6303},{x:0.05,y:1.6137}
kev_vs_rho.minX = 0.01;
kev_vs_rho.maxX = 0.99;
kev_vs_rho.minY = 0.01;
kev_vs_rho.maxY = 33.66;
< / script>

< div id =chart1> < / div>
< script>

use strict;

var Chart = function(_width,_height,_data,_div){
this.data = _data;
this.div = _div;

this.idx1 = 0;
this.select1 = 0;

this.margin = {top:30,right:30,bottom:30,left:80};
this.width = _width - this.margin.left - this.margin.right;
this.height = _height - this.margin.top - this.margin.bottom;

this.xscale = d3.scale.linear()
.domain([this.data.minX,this.data.maxX])
.range([ this.width]);

this.yscale = d3.scale.linear()
.domain([this.data.minY,this.data.maxY])
.range([this。 height,0]);

this.lineA = d3.svg.line()
.x(function(d){
console.log(this); //<是'path.line',而不是对象图
console.log(this.xscale); //<< == undefined

return this.xscale(dx); // << == undefined
})
.y(function(d){return this.yscale(dy);});

this.svg1 = d3.select(_div).append(div)。append(svg)
.datum(_data [this.select1] .values)
.attr(width,this.width + this.margin.left + this.margin.right)
.attr(height,this.height + this.margin.top + this.margin。 bottom)
.append(g)
.attr(transform,translate(+ this.margin.left +,+ this.margin.top +));

this.lineB = this.svg1.append(path)
.attr(class,line)
.datum(this.data [this。 select1] .values)
.attr(d,this.lineA);
};

var chart1 = new Chart(960,400,kev_vs_rho,#chart1);

< / script>
< / body>
< / html>除非您使用 / code>直接在Chart构造函数中(不是作为参数传递的匿名函数)或Chart对象的方法,或手动使用 call 应用 与聊天对象不会引用Chart对象。



您可以做的是显式设置一个变量到图表对象,在函数中使用它。

 < script& 

use strict;

var Chart = function(_width,_height,_data,_div){
self = this;
this.data = _data;
this.div = _div;

this.idx1 = 0;
this.select1 = 0;

this.margin = {top:30,right:30,bottom:30,left:80};
this.width = _width - this.margin.left - this.margin.right;
this.height = _height - this.margin.top - this.margin.bottom;

this.xscale = d3.scale.linear()
.domain([this.data.minX,this.data.maxX])
.range([ this.width]);

this.yscale = d3.scale.linear()
.domain([this.data.minY,this.data.maxY])
.range([this。 height,0]);

this.lineA = d3.svg.line()
.x(function(d){
console.log(self); //<< == is'path.line',not object Chart
console.log(self.xscale); //<< == undefined

return self.xscale(dx); // << == undefined
})
.y(function(d){return self.yscale(dy);});

this.svg1 = d3.select(_div).append(div)。append(svg)
.datum(_data [this.select1] .values)
.attr(width,this.width + this.margin.left + this.margin.right)
.attr(height,this.height + this.margin.top + this.margin。 bottom)
.append(g)
.attr(transform,translate(+ this.margin.left +,+ this.margin.top +));

this.lineB = this.svg1.append(path)
.attr(class,line)
.datum(this.data [this。 select1] .values)
.attr(d,this.lineA);
};

var chart1 = new Chart(960,400,kev_vs_rho,#chart1);

< / script>


I am having an issue with the following code. On the lines with the "console.log", the 'this' variable should contain 'Object Chart', but, instead, contains 'path.line'. Hence, the reference to xscale is undefined. Why am I getting this? This error occurs under Chrome and Firefox.

Thank you.

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="utf-8">
<style>
body {
  font: 10px sans-serif;
}

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 3.0px;
}
</style>
</head>
<body>
<script src="d3.v2.js"></script>
<script>
var kev_vs_rho= [{ 
values: [{x: 0.01, y: 0.2058},{x: 0.03, y: 0.2039},{x: 0.05, y: 0.2020}] }, {
values: [{x: 0.01, y: 1.6468},{x: 0.03, y: 1.6303},{x: 0.05, y: 1.6137}] }, ];
kev_vs_rho.minX=0.01;
kev_vs_rho.maxX=0.99;
kev_vs_rho.minY=0.01;
kev_vs_rho.maxY=33.66;
</script>

<div id="chart1"> </div>
<script>

"use strict";

var Chart = function ( _width, _height, _data, _div ) {
    this.data = _data;
    this.div = _div;

    this.idx1 = 0;
    this.select1 = 0;

    this.margin = {top: 30, right: 30, bottom: 30, left: 80};
    this.width = _width - this.margin.left - this.margin.right;
    this.height = _height - this.margin.top - this.margin.bottom;

    this.xscale = d3.scale.linear()
        .domain([this.data.minX, this.data.maxX])
        .range([0, this.width]);

    this.yscale = d3.scale.linear()
        .domain([this.data.minY, this.data.maxY])
        .range([this.height, 0]);

    this.lineA = d3.svg.line()
        .x(function (d) {
             console.log( this ); // <<== is 'path.line', not object Chart
             console.log( this.xscale ); // <<== undefined

             return this.xscale(d.x); // <<== undefined
             })
        .y(function (d) { return this.yscale(d.y); });

    this.svg1 = d3.select(_div).append("div").append("svg")
        .datum(_data[this.select1].values)
        .attr("width", this.width + this.margin.left + this.margin.right)
        .attr("height", this.height + this.margin.top + this.margin.bottom)
        .append("g")
        .attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")");

    this.lineB = this.svg1.append("path")
        .attr("class", "line")
        .datum(this.data[this.select1].values)
        .attr("d", this.lineA);
};

var chart1 = new Chart( 960, 400, kev_vs_rho, "#chart1");

</script>
</body>
</html>

解决方案

Unless you use this directly in the Chart constructor(not in an anonymous function passed as a parameter) or in a method of a Chart object or manually use call or apply with an Chat object, this wouldn't reference a Chart object.

What you can do is explicitly set a variable to the chart object and use that in the functions.

<script>

"use strict";

var Chart = function ( _width, _height, _data, _div ) {
    self = this;
    this.data = _data;
    this.div = _div;

    this.idx1 = 0;
    this.select1 = 0;

    this.margin = {top: 30, right: 30, bottom: 30, left: 80};
    this.width = _width - this.margin.left - this.margin.right;
    this.height = _height - this.margin.top - this.margin.bottom;

    this.xscale = d3.scale.linear()
        .domain([this.data.minX, this.data.maxX])
        .range([0, this.width]);

    this.yscale = d3.scale.linear()
        .domain([this.data.minY, this.data.maxY])
        .range([this.height, 0]);

    this.lineA = d3.svg.line()
        .x(function (d) {
             console.log( self ); // <<== is 'path.line', not object Chart
             console.log( self.xscale ); // <<== undefined

             return self.xscale(d.x); // <<== undefined
             })
        .y(function (d) { return self.yscale(d.y); });

    this.svg1 = d3.select(_div).append("div").append("svg")
        .datum(_data[this.select1].values)
        .attr("width", this.width + this.margin.left + this.margin.right)
        .attr("height", this.height + this.margin.top + this.margin.bottom)
        .append("g")
        .attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")");

    this.lineB = this.svg1.append("path")
        .attr("class", "line")
        .datum(this.data[this.select1].values)
        .attr("d", this.lineA);
};

var chart1 = new Chart( 960, 400, kev_vs_rho, "#chart1");

</script>

这篇关于为什么“这个”变量不是期望值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆