Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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_Jquery - Fatal编程技术网

Javascript 获取父类属性

Javascript 获取父类属性,javascript,jquery,Javascript,Jquery,我有一个javascript类,它有一个使用jQuery发送Ajax请求和处理响应的方法 我遇到的问题是,我不知道如何从jQuery函数中获取初始父类的属性。我尝试了$(this).parent(),但由于某些原因,它没有得到我所需要的 我的代码如下。有人能告诉我如何从这个循环到达基类吗 function companiesPage() { this.childCategoriesSelectid = '#childCategoryid'; this.setChildCateg

我有一个javascript类,它有一个使用jQuery发送Ajax请求和处理响应的方法

我遇到的问题是,我不知道如何从jQuery函数中获取初始父类的属性。我尝试了
$(this).parent()
,但由于某些原因,它没有得到我所需要的

我的代码如下。有人能告诉我如何从这个循环到达基类吗

function companiesPage()
{
    this.childCategoriesSelectid = '#childCategoryid';

    this.setChildCategories = function()
    {
        $.ajax({
            url: this.url,
            dataType: 'json',
            success: function(data)
            {
                $.each(data.childCategories, function()
                {
                    $($(this).parent().childCategoriesSelectid)//problem here
                    .append(
                        $('<option></option>')
                        .attr('value', this.childCategoryid)
                        .text(this.name)
                    );
                });
            }
        });
    }
}
功能公司页面()
{
this.childcategoriseselected='childcategorid';
this.setChildCategories=函数()
{
$.ajax({
url:this.url,
数据类型:“json”,
成功:功能(数据)
{
$.each(data.childCategories,function()
{
$($(this.parent().childCategoriseSelected)//这里有问题
.附加(
$('')
.attr('value',this.childCategoryid)
.text(此.name)
);
});
}
});
}
}
在异步Ajax调用之前捕获
Ajax成功处理程序是异步执行的,因此会释放其被调用方上下文(或作用域)。您必须捕获该自由变量,并在函数闭包中使用捕获的变量

您似乎希望
这个
有两种含义
  • 当您使用
    $(this.parent()…
    时,您希望
    this
    是一些
    HTMLDOMELENT
  • 使用
    this.name
    时,您希望
    this
    数据中的一个对象项(可能是数组)
  • 获取对象实例引用的解决方案 很明显,companiesPage是您的类(应该是Pascal大小写,而不是驼峰大小写)。因此在中使用
    。您可能通过以下方式创建它:

    var obj = new CompaniesPage();
    obj.setChildCategories();
    
    然后,您的代码应该如下所示:

    var CompaniesPage = function()
    {
        // keep this as public object property
        this.childCategoriesSelectId = "#childCategoryid";
    
        // keep this as public object method
        this.setChildCategories = function() {
            // private reference to object instance for later reuse
            var me = this;
            $.ajax({
                url: this.url, // not sure where this one's coming from
                dataType: 'json',
                success: function(data) {
                    $.each(data.childCategories, function() {
                        $(me.childCategoriesSelectId).append(
                            $('<option></option>')
                                .attr('value', this.childCategoryid)
                                .text(this.name)
                        );
                    });
                }
            });
        }
    };
    
    var CompaniesPage=function()
    {
    //将此保留为公共对象属性
    this.childcategoriseselected=“#childcategoriid”;
    //将其保留为公共对象方法
    this.setChildCategories=函数(){
    //对对象实例的私有引用,以供以后重用
    var me=这个;
    $.ajax({
    url:this.url,//不确定这个来自哪里
    数据类型:“json”,
    成功:功能(数据){
    $.each(data.childCategories,function(){
    $(me.ChildCategoriseSelected)。附加(
    $('')
    .attr('value',this.childCategoryid)
    .text(此.name)
    );
    });
    }
    });
    }
    };
    
    额外优化您的课程 您还必须意识到,您定义类级方法的方式并没有内存效率,因为单个对象实例不会共享同一个方法,而是希望每个对象实例都有自己的方法,这反映在内存资源上,特别是当您创建同一类的多个实例时

    这是您的课程的优化版本:

    var CompaniesPage = function() {
        // keep this as public object property
        this.childCategoriesSelectId = "#childCategoryid";
    }
    
    CompaniesPage.prototype.setChildCategories = function() {
        // private reference to me as object instance
        var me = this;
    
        $.ajax({
            url: this.url, // not sure where this one's coming from
            dataType: 'json',
            success: function(data) {
                $.each(data.childCategories, function() {
                    $(me.childCategoriesSelectId).append(
                        $('<option></option>')
                            .attr('value', this.childCategoryid)
                            .text(this.name)
                    );
                });
            }
        });
    };
    
    var CompaniesPage=function(){
    //将此保留为公共对象属性
    this.childcategoriseselected=“#childcategoriid”;
    }
    CompaniesPage.prototype.setChildCategories=函数(){
    //将我作为对象实例的私有引用
    var me=这个;
    $.ajax({
    url:this.url,//不确定这个来自哪里
    数据类型:“json”,
    成功:功能(数据){
    $.each(data.childCategories,function(){
    $(me.ChildCategoriseSelected)。附加(
    $('')
    .attr('value',this.childCategoryid)
    .text(此.name)
    );
    });
    }
    });
    };
    
    parent()
    用于DOM遍历。您遇到的是范围界定问题

    this
    关键字在输入新功能范围时并不总是引用同一对象。使用jQuery的方法时就是这种情况,该方法将
    this
    设置为要迭代的对象/元素

    看起来您使用的是构造函数实例模式,因此您只需存储对原始对象的引用,
    this
    关键字通过var声明引用(实例)。此引用在范围链中保持不变:

    function companiesPage()
    {
        var _this = this; //stores a reference to this instance object
        this.childCategoriesSelectid = '#childCategoryid';
    
        this.setChildCategories = function()
        {
            $.ajax({
                url: this.url,
                dataType: 'json',
                success: function(data)
                {
                    $.each(data.childCategories, function()
                    {
                        $(_this.childCategoriesSelectid) //NO problem here :)
                        .append(
                            $('<option></option>')
                            .val(this.childCategoryid)
                            .text(this.name)
                        );
                    });
                }
            });
        }
    }
    
    功能公司页面()
    {
    var _this=this;//存储对此实例对象的引用
    this.childcategoriseselected='childcategorid';
    this.setChildCategories=函数()
    {
    $.ajax({
    url:this.url,
    数据类型:“json”,
    成功:功能(数据)
    {
    $.each(data.childCategories,function()
    {
    $(\u this.childcategoriseselected)//这里没问题:)
    .附加(
    $('')
    .val(此.childCategoryid)
    .text(此.name)
    );
    });
    }
    });
    }
    }
    
    这样,您就可以访问实例中任何位置的任何实例的公共方法和属性


    ps.按照惯例,InstanceTables/构造函数的第一个字母通常大写。

    不要使用
    .attr('value',…)
    。改用
    .val(..)
    !HTML并不重要。如果我试图提醒()它,它会显示“undefined”,因此它显然不在那里。是的,但在这种情况下,他在
    $中使用
    this
    。each()
    引用迭代中的当前项。(我希望人们只使用参数!)…事实上,我不知道他希望
    这个
    对于
    $(this.parent()
    调用是什么。我在代码中没有看到任何有意义的
    这个
    。@user1689607:我也看到了。我们不能